Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2484

Lazy loaded associated entities do not trigger the postLoad event

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: Git Master
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:

      Description

      An entity retrieved with $entity = $em->find will correctly trigger the postLoad event but an entity associated with that entity $association = $entity->association which is lazy loaded will retrieve and return the associated entity without triggering postLoad. Eager loading does follow the lifecycle path of triggering postLoad.

      Functional Test:
      https://github.com/doctrine/doctrine2/pull/685

        Activity

        Hide
        Fabio B. Silva added a comment - - edited

        Hi Tom,

        You CANNOT check the entity class by using get_class, doctrine uses the pattern proxy to delay the load from database.

        Then $args->getEntity() might be a different class that extends your entity
        In this case you should always check using instanceof :

        $entity = $args->getEntity();
        
        if ($entity instanceof DDC2484_Car) {
            $entity->setBrand('BMW');
        }
        

        Also when you try to load the same entity more than once :

        $eagerDriver->getCar();
        $lazyDriver->getCar();
        

        The entity will be cached at the first time, triggering onLoad just once.

        It means that the entity will be always the same instance,
        it's not loaded from database untill you clean up the entity manager.

        assertSame($eagerDriver->getCar(), $lazyDriver->getCar());
        Show
        Fabio B. Silva added a comment - - edited Hi Tom, You CANNOT check the entity class by using get_class, doctrine uses the pattern proxy to delay the load from database. Then $args->getEntity() might be a different class that extends your entity In this case you should always check using instanceof : $entity = $args->getEntity(); if ($entity instanceof DDC2484_Car) { $entity->setBrand('BMW'); } Also when you try to load the same entity more than once : $eagerDriver->getCar(); $lazyDriver->getCar(); The entity will be cached at the first time, triggering onLoad just once. It means that the entity will be always the same instance, it's not loaded from database untill you clean up the entity manager. assertSame($eagerDriver->getCar(), $lazyDriver->getCar());
        Hide
        Benjamin Eberlei added a comment -

        Wrong assumption using `get_class`

        Show
        Benjamin Eberlei added a comment - Wrong assumption using `get_class`

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Tom Anderson
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: