public function postLoad(LifecycleEventArgs $eventArgs) { $em = $eventArgs->getEntityManager(); $uow = $em->getUnitOfWork(); $entity = $eventArgs->getEntity(); $classname = get_class($entity); if (!array_key_exists($classname, $this->getEntities())) { return null; } /** @var Schema $schema */ $schema = $em->getRepository('Padam87AttributeBundle:Schema')->findOneBy(array('className' => $classname)); if ($schema === null) { throw new \UnexpectedValueException('Schema not found for ' . $classname); } $qb = $em->getRepository($classname)->createQueryBuilder('main'); $qb->distinct()->select('d.id')->join('main.attributes', 'a')->join('a.definition', 'd', null, null, 'd.id')->where('main = :main')->setParameter('main', $entity); $definitions = $qb->getQuery()->getScalarResult(); $ids = array_map('current', $definitions); foreach ($schema->getDefinitions() as $definition) { if (!in_array($definition->getId(), $ids)) { $attribute = new Attribute(); $attribute->setDefinition($definition); $entity->addAttribute($attribute); } } if ($uow->getEntityState($entity) == UnitOfWork::STATE_MANAGED) { $em->persist($entity); $em->flush($entity); } }
/** * @param Attribute $attributes */ public function removeAttribute(Attribute $attributes) { $this->attributes->removeElement($attributes); }