em = $entityManager; } public static function getSubscribedEvents() { return [ EntityManagerEvent::PRE_CREATE_EVENT => ['setCommonProperty'], ]; } public function setCommonProperty(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); } }