简体   繁体   English

zf2 doctrine2 editAction

[英]zf2 doctrine2 editAction

I am facing an error to edit the values of my Entity Category. 我在编辑“实体类别”的值时遇到错误。 Here is my code 这是我的代码

<a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => $child->getId()));?>">Bearbeiten</a>

In my IndexController is the Action: 在我的IndexController中是Action:

public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id');

        $em = $this->getEntityManager();
        $category = $em->getRepository('Application\Entity\Category')->find($id);

        $form = new CategoryForm();
        $form->setHydrator(new CategoryHydrator());
        $form->bind($category);

        $request = $this->getRequest();

        if($request->isPost())
        {

            $form->setData($this->getRequest()->getPost());
            if($form->isValid())
            {   
                $object = $form->getData();
                $category->setName($object->getName());
                $category->setDescription($object->getDescription());
                $category->setParent($object->getParent());
                $em->flush();
                return $this->redirect()->toRoute('home');
            }   
        }

        return array(
                            'form' => $form
                    );  
    }

Here is my CategoryForm.php: 这是我的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',
            ),
        ));

    }
}

And the following is my edit.phtml: 以下是我的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();
?>

By calling the action 'edit', the browser shows a error with the message: 通过调用操作“ edit”,浏览器将显示以下错误消息:

Object provided to Escape helper, but flags do not allow recursion

Does someone knows my mistake and can help me? 有人知道我的错误并可以帮助我吗?

First, from your question it can only be quessed what the $child->getId() is. 首先,从您的问题开始,只能查询$ child-> getId()是什么。 I assume the $child is an instance of the Category class, and getId() returns an integer, being the category's id. 我假设$ child是Category类的实例,而getId()返回一个整数,即类别的ID。 So instead, i just put this, for testing, in my index.phtml: 因此,我只是将其放在我的index.phtml中进行测试:

<a href="<?php echo $this->url('application/default', array('action'=>'edit','id' => 1));?>">Bearbeiten</a>

To be on the safe side, I have also passed an instance of Category to index.phtml as $child variable, used your code for the link, and the link worked, (so it's not something wrong with url view helper). 为了安全起见,我还将Category的实例作为$ child变量传递给index.phtml,将您的代码用于链接,并且链接有效(因此url view helper没什么问题)。 Anyway, I could just write the url into the browser and click enter, but since I don't know where your error showed up, (perhaps after clicking the link), I had to make sure :) 无论如何,我可以将URL写入浏览器并单击Enter,但是由于我不知道您的错误出现在什么地方(也许在单击链接之后),因此我必须确保:)

Next, I copied your code exactly, except for this line: 接下来,我完全复制了您的代码,但以下行除外:

$form->setHydrator(new CategoryHydrator());

You did not provide the code for your hydrator. 您未提供您的饮水机代码。 I assume you've created it, but without it being shown to me, I can only assume there is an error somewhere in the hydrator code. 我假设您已经创建了它,但是如果没有显示给我,我只能假设在水化器代码中某处存在错误。 Instead of that line, try: 代替该行,请尝试:

$form->setHydrator ( new \DoctrineModule\Stdlib\Hydrator\DoctrineObject ( $em, 'Application\Entity\Category' ) );

I hope I can safely assume that DoctrineModule was installed with composer and is added to your 'modules' array in application.config.php. 希望我可以放心地认为DoctrineModule是随composer一起安装的,并已添加到application.config.php中的“模块”数组中。

With this line, otherwise the code being exactly as yours, after a click on the link, or entering the URI /application/edit/1 ( 1 being the id of my category to edit), the form was displayed and the input fields were populated correctly, without any errors. 在这一行中,否则,在单击链接或输入URI / application / edit / 1(1是要编辑的类别的ID)后,代码与您的代码完全相同,则显示该表单,并且输入字段为正确填充,没有任何错误。

If there's still something wrong, perhaps there is some error in your Category entity. 如果仍有问题,则类别实体中可能存在错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM