/** * Update relations between Identity entity and Organization entity * * @param IdentityInterface $model */ protected function updateIdentityOrganizationsRelations($model) { $orgas = array(); $form_identity_orgas = $model->getOrganizations(); if (is_null($form_identity_orgas)) { return; } foreach ($form_identity_orgas as $identity_orga) { if (!isset($orgas[$identity_orga->getOrganization()->getId()])) { $orgas[$identity_orga->getOrganization()->getId()] = 1; } else { $model->removeOrganization($identity_orga); } } // Detect relations removed $relations = $this->container->get('asf_contact.identity_organization.manager')->getRepository()->findBy(array('member' => $model->getId())); foreach ($relations as $relation) { $found = false; foreach ($model->getOrganizations() as $identity_organization) { if ($relation->getOrganization()->getId() == $identity_organization->getOrganization()->getId()) { $found = true; } } if (false === $found) { $this->container->get('asf_contact.identity_organization.manager')->getEntityManager()->remove($relation); } } // Detect new relations foreach ($model->getOrganizations() as $identity_organization) { $found = false; foreach ($relations as $relation) { if ($relation->getOrganization()->getId() == $identity_organization->getOrganization()->getId()) { $found = true; } } if (false === $found) { $identity_organization->setMember($model); $this->container->get('asf_contact.identity.manager')->getEntityManager()->persist($identity_organization); } } }