Doctrine postLoad event for associations

后端 未结 1 831
轻奢々
轻奢々 2020-12-21 00:30

I currently have an entity which I would like to modify slightly upon load. This modification will be a one time change which will then be persisted in a new field along wit

1条回答
  •  囚心锁ツ
    2020-12-21 00:46

    I was able to load the associated Location objects within the postLoad event by using the initializeObject() method on the Entity Manager.

    /**
     * Upon loading the object, if the location text isn't set, set it
     * @param \Doctrine\ORM\Event\LifecycleEventArgs $args
     */
    public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $args)
    {
        $this->em = $args->getEntityManager();
        $entity = $args->getEntity();
    
        if ($entity instanceof \Entities\Location)
        {
            if (is_null($entity->getPathText()))
            {
                $entity->setPathText("new value");
                $this->em->flush($entity);
            }
        } elseif ($entity instanceof {parent Entity})
        {
            $location = $entity->getLocation();
            // This triggers the postLoad event again, but this time with Location as the parent Entity
            $this->em->initializeObject($location);
        }
    }
    

    0 讨论(0)
提交回复
热议问题