handleAfterSaveContacts($entity, $options); return $result; } protected function handleAfterSaveContacts(Entity $entity, array $options) { $contactIdChanged = $entity->has('contactId') && $entity->get('contactId') != $entity->getFetched('contactId'); if ($contactIdChanged) { $contactId = $entity->get('contactId'); if (empty($contactId)) { $this->unrelate($entity, 'contacts', $entity->getFetched('contactId')); return; } } if ($contactIdChanged) { $pdo = $this->getEntityManager()->getPDO(); $sql = " SELECT id FROM case_contact WHERE contact_id = ".$pdo->quote($contactId)." AND case_id = ".$pdo->quote($entity->id)." AND deleted = 0 "; $sth = $pdo->prepare($sql); $sth->execute(); if (!$sth->fetch()) { $this->relate($entity, 'contacts', $contactId); } } } }