/** * removes the address relation from a contact and also deletes the address if it has no more relations. * * @param AccountInterface $account * @param AccountAddressEntity $accountAddress * * @return mixed|void * * @throws \Exception */ public function removeAddressRelation($account, $accountAddress) { if (!$account || !$accountAddress) { throw new \Exception('Account and AccountAddress cannot be null'); } // reload address to get all data (including relational data) /** @var AddressEntity $address */ $address = $accountAddress->getAddress(); $address = $this->em->getRepository('SuluContactBundle:Address')->findById($address->getId()); $isMain = $accountAddress->getMain(); // remove relation $address->removeAccountAddress($accountAddress); $account->removeAccountAddress($accountAddress); // if was main, set a new one if ($isMain) { $this->setMainForCollection($account->getAccountContacts()); } // delete address if it has no more relations if (!$address->hasRelations()) { $this->em->remove($address); } $this->em->remove($accountAddress); }