Php zf2 doctrine2编辑操作

Php zf2 doctrine2编辑操作,php,doctrine-orm,zend-framework2,Php,Doctrine Orm,Zend Framework2,编辑实体类别的值时遇到错误。这是我的密码 <a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => $child->getId()));?>">Bearbeiten</a> 以下是我的CategoryForm.php: namespace Application\Form; use Zend\Form\Form; cla

编辑实体类别的值时遇到错误。这是我的密码

<a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => $child->getId()));?>">Bearbeiten</a>
以下是我的CategoryForm.php:

namespace Application\Form;

use Zend\Form\Form;

class CategoryForm extends Form
{
    public function __construct()
    {
        parent::__construct('categoryForm');

        $this->setAttribute('method', 'post');

        $this->add(array(
            'name' => 'name',
            'attributes' => array(
                'type' => 'text',
                'id' => 'name',
            ),
            'options' => array(
                'label' => 'Ihr Name:',
            ),
        ));

        $this->add(array(
            'name' => 'description',
            'attributes' => array(
                'type' => 'text',
                'id' => 'description',
            ),
            'options' => array(
                    'label' => 'Ihre Beschreibung:',
            ),
        ));

        $this->add(array(
                'name' => 'parent',
                'attributes' => array(
                        'id' => 'parent',
                        'name' => 'parent',
                        'type' => 'hidden',
                ),
        ));

        $this->add(array(
            'name'=>'submit',
            'attributes'=>array(
                        'type' => 'submit',
                        'value' => 'OK',
            ),
        ));

    }
}
下面是my edit.phtml:

<?php
 $form = $this->form;

 $form->prepare();

 echo $this->form()->openTag($form);
 echo $this->formRow($form->get('name'));
 echo $this->formRow($form->get('description'));
 echo $this->formRow($form->get('parent'));
 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();
?>

有人知道我的错误并能帮助我吗?

首先,从你的问题来看,只能询问$child->getId()是什么。我假设$child是Category类的一个实例,getId()返回一个整数,它是该类别的id。因此,为了测试,我将它放在我的index.phtml中:

<a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => 1));?>">Bearbeiten</a>
您没有提供水合器的代码。我假设您已经创建了它,但是没有向我展示它,我只能假设代码中的某个地方有错误。请尝试以下操作,而不要使用该行:

$form->setHydrator ( new \DoctrineModule\Stdlib\Hydrator\DoctrineObject ( $em, 'Application\Entity\Category' ) );
我希望我可以安全地假设doctriemodule是与composer一起安装的,并添加到application.config.php中的“modules”数组中

使用这一行,否则代码与您的代码完全相同,单击链接或输入URI/application/edit/1(1是要编辑的类别的id)后,将显示表单并正确填充输入字段,没有任何错误

如果仍然有错误,可能是类别实体中有错误

<a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => 1));?>">Bearbeiten</a>
$form->setHydrator(new CategoryHydrator());
$form->setHydrator ( new \DoctrineModule\Stdlib\Hydrator\DoctrineObject ( $em, 'Application\Entity\Category' ) );