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

前端 未结 2 468
悲哀的现实
悲哀的现实 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: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)
    }
    

提交回复
热议问题