em = $entityManager; } public static function getSubscribedEvents() { return [ EntityManagerEvent::CREATE_EVENT => ['createEntity'], ]; } public function createEntity(EntityManagerEvent $event) { $entity = $event->getEntity(); $entityRepository = $this->em->getRepository(get_class($entity)); if ($entity instanceof SortableInterface) { $this->setSortableProperty($entity, $entityRepository); } } private function setSortableProperty(EntityInterface $entity, AbstractRepositoryInterface $entityRepository) { if ($entity instanceof StatusInterface) { $countParam['status'] = 1; } if ($entity instanceof TreeInterface) { if($entity->getParent()){ $countParam['parent'] = $entity->getParent()->getId(); }else{ $countParam['parent'] = null; } } $total = $entityRepository->count($countParam); $entity->setPosition($total); } }