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?
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