public function regenerateProgenitorNewsletter(Progenitor $progenitor) { $addGroups = array(); $removeGroups = array(); $searchStrings = array(); foreach ($progenitor->getEstudiantes() as $estudiante) { $searchStrings = array_merge($searchStrings, $this->retrieveEstudianteSearchList($estudiante)); } $groupsLists = array(); foreach ($searchStrings as $searchString) { $group = $this->em->getRepository('MaithNewsletterBundle:UserGroup')->findOneBy(array('name' => $searchString)); if (!$group) { $this->logger->error(sprintf("Group %s SHOULD exits for this user %s.", $searchString, $estudiante->getId())); } else { $groupsLists[$group->getId()] = $group; } } $newsLetterUser = $progenitor->getNewsletterUser(); if ($newsLetterUser) { foreach ($groupsLists as $newGroups) { if (!$newsLetterUser->getUserGroups()->contains($newGroups)) { $addGroups[] = $newGroups; } } foreach ($newsLetterUser->getUserGroups() as $userGroup) { if (!isset($groupsLists[$userGroup->getId()])) { $removeGroups[] = $userGroup; } } foreach ($addGroups as $newGroups) { $newsLetterUser->addUserGroup($newGroups); } foreach ($removeGroups as $newGroups) { $newsLetterUser->removeUserGroup($newGroups); } $this->em->persist($newsLetterUser); $this->em->flush(); } else { $this->logger->info(sprintf("The parent with id: %s and email :% don't belong to newsletters", $progenitor->getId(), $progenitor->getEmail())); } }