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);
}