/**
  * Set account_id for owners in $this->owners array
  *
  * @param ClientAccount $account
  */
 protected function saveAccountOwners(ClientAccount $account)
 {
     $ownerTypes = $this->getOwnerTypes();
     if (empty($ownerTypes) && $account->getAccountOwners()->isEmpty()) {
         $owner = new ClientAccountOwner();
         $owner->setOwnerType(ClientAccountOwner::OWNER_TYPE_SELF);
         $owner->setClient($account->getClient());
         $owner->setAccount($account);
         $this->em->persist($owner);
         $this->em->flush();
     } else {
         foreach ($account->getAccountOwners() as $accountOwner) {
             $this->em->remove($accountOwner);
         }
         foreach ($ownerTypes as $type) {
             $this->createAccountOwnerByType($account, $type);
         }
         $this->em->persist($account);
         $this->em->flush();
     }
 }
 /**
  * Set account_id for owners in $this->owners array
  *
  * @param ClientAccount $account
  */
 protected function saveAccountOwners(ClientAccount $account)
 {
     if (empty($this->owners) && $account->getAccountOwners()->isEmpty()) {
         $owner = new ClientAccountOwner();
         $owner->setOwnerType(ClientAccountOwner::OWNER_TYPE_SELF);
         $owner->setClient($account->getClient());
         $owner->setAccount($account);
         $account->addAccountOwner($owner);
         $this->em->persist($owner);
     } else {
         $repo = $this->em->getRepository('WealthbotClientBundle:ClientAccountOwner');
         foreach ($this->owners as $ownerItem) {
             $owner = new ClientAccountOwner();
             $owner->setOwnerType($ownerItem['owner_type']);
             $owner->setAccount($account);
             if ($ownerItem['owner_type'] === ClientAccountOwner::OWNER_TYPE_SELF) {
                 $client = $this->em->getRepository('WealthbotUserBundle:User')->find($ownerItem['owner_client_id']);
                 if ($client) {
                     $exist = $repo->findOneBy(array('owner_type' => $ownerItem['owner_type'], 'owner_client_id' => $client->getId(), 'account_id' => $account->getId()));
                     if (!$exist) {
                         $owner->setClient($client);
                         $this->em->persist($owner);
                     }
                 }
             } else {
                 $contact = $this->em->getRepository('WealthbotClientBundle:ClientAdditionalContact')->find($ownerItem['owner_contact_id']);
                 if ($contact) {
                     $exist = $repo->findOneBy(array('owner_type' => $ownerItem['owner_type'], 'owner_contact_id' => $contact->getId(), 'account_id' => $account->getId()));
                     if (!$exist) {
                         $owner->setContact($contact);
                         $this->em->persist($owner);
                     }
                 }
             }
             $account->addAccountOwner($owner);
         }
     }
     $this->em->persist($account);
     $this->em->flush();
 }