handleAfterSaveAccounts($entity, $options); if ($entity->has('targetListId') && $entity->isNew()) { $this->relate($entity, 'targetLists', $entity->get('targetListId')); } return $result; } protected function handleAfterSaveAccounts(Entity $entity, array $options) { $accountIdChanged = $entity->has('accountId') && $entity->get('accountId') != $entity->getFetched('accountId'); $titleChanged = $entity->has('title') && $entity->get('title') != $entity->getFetched('title'); if ($accountIdChanged) { $accountId = $entity->get('accountId'); if (empty($accountId)) { $this->unrelate($entity, 'accounts', $entity->getFetched('accountId')); return; } } if ($titleChanged) { if (empty($accountId)) { $accountId = $entity->getFetched('accountId'); if (empty($accountId)) { return; } } } if ($accountIdChanged || $titleChanged) { $pdo = $this->getEntityManager()->getPDO(); $sql = " SELECT id, role FROM account_contact WHERE account_id = ".$pdo->quote($accountId)." AND contact_id = ".$pdo->quote($entity->id)." AND deleted = 0 "; $sth = $pdo->prepare($sql); $sth->execute(); if ($row = $sth->fetch()) { if ($titleChanged && $entity->get('title') != $row['role']) { $this->updateRelation($entity, 'accounts', $accountId, array( 'role' => $entity->get('title') )); } } else { if ($accountIdChanged) { $this->relate($entity, 'accounts', $accountId, array( 'role' => $entity->get('title') )); } } } } }