Lazy-loading properties not loading in Doctrine 2.

2019-02-19 14:20发布

I'm using PHP and Doctrine 2.0. All entities work fine, with the exception of the entity-relation detailed below (or other entities are failing where I'm not noticing it).


Consider the following entities:

/** @Entity */
class Target {
  /**
   * @ManyToOne(targetEntity="k\entity\Source", cascade={"persist"})
   * @JoinColumn(name="basic_vacancy_id", nullable=false)
   * @var \k\entity\Source
   */
  $source;

  ...
}

/** @Entity */
class Source {

  ...
}

Now, when I call $target->getSource() I get an instance of k\entity\proxy\kentitySourceProxy (which is the correct proxy class). However, all the getters for Source's properties return NULL.

What could I be doing wrong?


I've added the fetch="EAGER" attribute to the relational annotation, and now everything seems to go just fine (except for the fact that loading is no longer lazy). What could cause Doctrine 2.0's lazy loading to break?

3条回答
戒情不戒烟
2楼-- · 2019-02-19 14:24

As seen above in my question, I've added the fetch="EAGER" attribute to the relational annotation, and now everything seems to go just fine (except for the fact that loading is no longer lazy).

This is of course a workaround, and no fix of the actual bug. In addition to this, I still don't know what caused my code/Doctrine to break. However, the performance impact of this workaround seems to be negligible - if even present at all.

查看更多
Juvenile、少年°
3楼-- · 2019-02-19 14:29

I have a very similart problem, reported here:-

Zend 1.11.11 Doctrine 2.1.2 initalising of associative proxy entities

where do you add the fetch="EAGER" attribute?

查看更多
做自己的国王
4楼-- · 2019-02-19 14:32

It looks like internal d2 bug. Or you use reflection to retrieve properties of your object. Proxy classes use persister to initialize them on any method call. Does it issue a query when you use getter on this proxy?

查看更多
登录 后发表回答