How can public fields “break lazy loading” in Doctrine 2?

前端 未结 2 465
悲哀的现实
悲哀的现实 2020-12-09 19:57

When I run doctrine orm:validate-schema, it pops up a bunch of warnings about my mapped columns being public and not using getter/setter methods to wrap them. I

相关标签:
2条回答
  • 2020-12-09 20:35

    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.

    0 讨论(0)
  • 2020-12-09 20:50

    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)
    }
    
    0 讨论(0)
提交回复
热议问题