Doctrine 2 inject data to loaded models

2019-09-09 11:12发布

I have a Doctrine 2 model which has a relation to a file system model (MogileFS) which I've implemented using a model/mapper approach. What I'm trying to accomplish is to lazy load the non-doctrine model from a Doctrine 2 entity, and inject a mapper object into this model while doing so.

Example:

use Doctrine\ORM\Tools\Pagination\Paginator;
$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
$query = $entityManager->createQuery($dql)
                   ->setFirstResult(0)
                   ->setMaxResults(100);

$paginator = new Paginator($query, $fetchJoin = true);

$c = count($paginator);
foreach ($paginator as $post) {
  // TODO Should use injected mapper to do find() 
  // and lazy load model when not set
    echo $post->getThumbnailFileModel() . "\n";
}

How can I set up an entity loading hook, that will inject my modelMapper into the entity in order for the lazy loading to work?

1条回答
爷、活的狠高调
2楼-- · 2019-09-09 11:37

I managed to solve this by using @postLoad event listener.

For reference, see: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/events.html

查看更多
登录 后发表回答