Php 原则2-使用Zend表单在ObjectSelect元素上使用魔法getter

Php 原则2-使用Zend表单在ObjectSelect元素上使用魔法getter,php,forms,doctrine-orm,doctrine,zend-framework2,Php,Forms,Doctrine Orm,Doctrine,Zend Framework2,当使用DOCTRIONS的ObjectSelect用Zend表单填充表单元素时,property参数需要相应实体中的getPropertyName()方法 我们是否能够告诉ObjectSelect使用神奇的getter,例如\uu get('PropertyName'),而不是为实体中的每个受保护属性创建getter 原因是,如果一个表有+100个列,我们应该为每个列创建getter,还是可以使用神奇的getter来填充表单元素 实体 namespace Users\Entity; use D

当使用DOCTRIONS的
ObjectSelect
用Zend表单填充表单元素时,
property
参数需要相应实体中的
getPropertyName()
方法

我们是否能够告诉
ObjectSelect
使用神奇的getter,例如
\uu get('PropertyName')
,而不是为实体中的每个受保护属性创建getter

原因是,如果一个表有+100个列,我们应该为每个列创建getter,还是可以使用神奇的getter来填充表单元素

实体

namespace Users\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="tbl_users")
 * @property int $id
 * @property string $firstname
 */

class User{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $firstname;

    /**
     * Exposes protected properties
     *
     * @param string $prop
     */
    public function __get($prop){
        return $this->$prop;
    }

    /**
     * Saves protected properties
     *
     * @param string $prop
     * @param mixed $val
     */
    public function __set($prop, $val){
        $this->$prop = $val;
    }
}
表格

namespace Users\Form;

use Zend\Form\Form;

use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;

class UserForm extends Form implements ObjectManagerAwareInterface{

    protected $em;

    public function __construct(EntityManager $em, $name = null){

    parent::__construct('Edit User');

    $hydrator = new DoctrineHydrator($em, 'Users\Entity\User');
    $this->setHydrator($hydrator);

    $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'firstname',
        'options' => array(
            'object_manager' => $em,
            'target_class' => 'Users\Entity\User',
            'property' => 'firstname'
        )
    ));
}

由于这个问题没有得到解答,我认为关键在于在DoctrineHydrator构造函数中将“byValue”设置为false

$hydrator = new DoctrineHydrator($em, 'Users\Entity\User', false);
默认情况下,byValue为true,并指示条令使用getProperty()方法