How can public fields "break lazy loading" in Doctrine 2?
When I run doctrine orm:validate-schema
, it pops up a bunch of warnings about my mapped columns being 开发者_C百科public and not using getter/setter methods to wrap them. It says that they "break lazy loading". I can understand how making associated collections public could be problematic (I do make these private and wrap them), but how is this an issue for fields on the object? Fields are loaded in full, to my knowledge.
I'll give a shot at this although I'm certainly not a Doctrine2 expert.
From my (limited) usage and testing it seems Doctrine may give you a related object without loading the data for that object. At that point public properties will break lazy loading.
Doctrine is lazy loading at the point where the persisted data is requested, not when the object that contains persisted data is requested.
Update: I took a look at the actual proxy code and it seems my original understanding was mostly correct. The proxy object doesn't load itself until a method of the object is called. So any request to a public property would not load the data.
Note that Doctrine 2.4 now supports proxy objects for entites with public properties.
Marco Pivetta's website explains how it works:
class Customer {
public $name;
public $surname;
}
class CustomerProxy extends Customer {
public function __construct(Customer $customer) {
unset($this->name, $this->surname);
$this->customer = $customer;
}
public function __set($name, $value) {
$this->customer->$name = $value;
}
public function __get($name) {
return $this->customer->$name;
}
// __isset, __unset, __clone, __sleep, __wakeup (or serialize/unserialize)
}
精彩评论