如何使Doctrine Hydrator填充实体?

时间:2014-03-17 19:23:55

标签: zend-framework orm doctrine-orm

这是我到目前为止所拥有的,

形式:

use DoctrineModule\Stdlib\Hydrator\DoctrineObject as Hydrator;
class UserRegisterForm extends
Form implements InputFilterProviderInterface
{
    public function __construct($entityManager)
    {
        parent::__construct('Practitioner');

        $this
            ->setAttribute('method', 'post')
            ->setHydrator(new Hydrator($entityManager), 'App\Entity\Users', true)
            ->setObject(new Users())
        ;

        // add fields here ...
    }
}

控制器:

class UsersController extends AbstractActionController {

public function addAction()
{
    $request = $this->getRequest();
    $form = new PractitionerRegisterForm($this->getEntityManager());
    $users = new \App\Entity\Users;
    $form->bind($users);

    if ( $request->isPost() ) {
        $form->setData($request->getPost());
        // echo pre var_dump($this) /pre
        $users->getDetails();
    } else {
        // do something here
    }

    return ['form' => $form];
}

但我从$users->getDetails()得到的是带有NULL的属性

那么如何使用Hydrator来填充实体用户?

1 个答案:

答案 0 :(得分:0)

解决方案:我必须使用$form->isValid()验证表单,然后验证然后保存(填充)实体。