/** * Gets account by id - can include relations. * * @param $id * @param $locale * @param $includes * * @return Account * * @throws EntityNotFoundException */ public function getByIdAndInclude($id, $locale, $includes) { $account = $this->accountRepository->findAccountById($id, in_array('contacts', $includes)); if (!$account) { throw new EntityNotFoundException($this->accountRepository->getClassName(), $id); } return $this->accountFactory->createApiEntity($account, $locale); }
/** * @param Contact $contact * @param $data * * @throws EntityNotFoundException */ public function setMainAccount(Contact $contact, $data) { // set account relation if (isset($data['account']) && isset($data['account']['id']) && $data['account']['id'] != 'null') { $accountId = $data['account']['id']; $account = $this->accountRepository->findAccountById($accountId); if (!$account) { throw new EntityNotFoundException($this->accountRepository->getClassName(), $accountId); } // get position $position = null; if (isset($data['position'])) { $position = $this->getPosition($data['position']); } // check if relation between account and contact already exists $mainAccountContact = $this->getMainAccountContact($contact); $accountContact = $this->getAccounContact($account, $contact); // remove previous main accountContact if ($mainAccountContact && $mainAccountContact !== $accountContact) { // if this contact is the main-Contact - set mainContact to null if ($mainAccountContact->getAccount()->getMainContact() === $contact) { $mainAccountContact->getAccount()->setMainContact(null); } $this->em->remove($mainAccountContact); } // if account-contact relation existed set params if ($accountContact) { $accountContact->setMain(true); $accountContact->setPosition($position); } else { // else create new one $this->createMainAccountContact($contact, $account, $position); } } else { // if a main account exists - remove it if ($accountContact = $this->getMainAccountContact($contact)) { // if this contact is the main-Contact - set mainContact to null if ($accountContact->getAccount()->getMainContact() === $contact) { $accountContact->getAccount()->setMainContact(null); } $this->em->remove($accountContact); } } }