2014-02-24 38 views
0

我正面臨編輯我的實體類別值的錯誤。這裏是我的代碼zf2 doctrine2 editAction

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

在我的IndexController是動作:

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 
        ); 
    } 

這裏是我的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', 
      ), 
     )); 

    } 
} 

而下面是我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(); 
?> 

通過調用動作'編輯',瀏覽器顯示錯誤消息:

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

有人知道我的錯誤,可以幫助我嗎?

回答

0

首先,從你的問題來看,它只能被問及$ child-> getId()是什麼。我假設$ child是Category類的一個實例,並且getId()返回一個整數,該整數是該類別的ID。因此,不是,我只是把這個,進行測試,在我的index.phtml:

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

爲了安全起見,我還通過了分類的一個實例來index.phtml爲$孩子變量,使用您的鏈接的代碼和鏈接工作,(所以它沒有錯誤的URL視圖助手)。無論如何,我只需將網址寫入瀏覽器並點擊進入,但由於我不知道您的錯誤出現在哪裏(可能點擊鏈接後),我不得不確認:)

接下來,我請將您的代碼完全複製,但此行除外:

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

您沒有爲您的水合器提供代碼。我假設你已經創建了它,但是如果沒有顯示給我,我只能假設hydrator代碼中有某處出現錯誤。相反,該行的,請嘗試:

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

我希望我可以有把握地認爲DoctrineModule安裝與作曲家,並添加到您的application.config.php「模塊」陣列。

使用此行,否則代碼與您的完全相同,點擊鏈接後,或者輸入URI/application/edit/1(1是我要編輯的類別的ID),表單被顯示並且輸入字段填充正確,沒有任何錯誤。

如果仍有問題,可能是您的類別實體中存在一些錯誤。

相關問題