2014-02-27 3 views
0

그래서 나는 젠드 프레임 워크 프로젝트에서 일을하고 난 교리, 내가 만들 내 양식, 컨트롤러 및 엔티티를 사용하고 있지만, 젠드 프레임 워크에서 재귀를 허용하지 않습니다 이건 내 법인개체 도우미를 탈출하기 위해 제공하지만, 플래그 내가 실행할 때 2

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

입니다 : 내 프로젝트는이 오류가있어

namespace Application\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Zend\Form\Annotation; 

/** 
* Article 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Application\Entity\ArticleRepository") 
*/ 
class Article 
{ 
    /** 
    * @ORM\Column(name="publication", type="boolean") 
    */ 
    private $publication; 

    public function __construct() 
    { 
     $this->date = new \Datetime(); 
    } 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="string", length=255) 
    */ 
    private $title; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date", type="date") 
    */ 
    private $date; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="content", type="text") 
    */ 
    private $content; 

    /** 
    * @ORM\OneToOne(targetEntity="Application\Entity\Image", cascade={"persist","remove"}) 
    */ 
    private $image; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    * @return Article 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set date 
    * 
    * @param \DateTime $date 
    * @return Article 
    */ 
    public function setDate($date) 
    { 
     $this->date = $date; 

     return $this; 
    } 

    /** 
    * Get date 
    * 
    * @return \DateTime 
    */ 
    public function getDate() 
    { 
     return $this->date; 
    } 

    /** 
    * Set content 
    * 
    * @param string $content 
    * @return Article 
    */ 
    public function setContent($content) 
    { 
     $this->content = $content; 

     return $this; 
    } 

    /** 
    * Get content 
    * 
    * @return string 
    */ 
    public function getContent() 
    { 
     return $this->content; 
    } 

    /** 
    * Set publication 
    * 
    * @param boolean $publication 
    * @return Article 
    */ 
    public function setPublication($publication) 
    { 
     $this->publication = $publication; 

     return $this; 
    } 

    /** 
    * Get publication 
    * 
    * @return boolean 
    */ 
    public function getPublication() 
    { 
     return $this->publication; 
    } 

    /** 
    * Set image 
    * 
    * @param \Application\Entity\Image $image 
    * @return Article 
    */ 
    public function setImage(\Application\Entity\Image $image = null) 
    { 
     $this->image = $image; 

     return $this; 
    } 

    /** 
    * Get image 
    * 
    * @return \Application\Entity\Image 
    */ 
    public function getImage() 
    { 
     return $this->image; 
    } 

} 

을 그리고이 양식 위스콘신입니다 일 필드 유효성 검증

그런 다음
class ArticleForm extends Form implements ObjectManagerAwareInterface 

{ 
    /** 
    * @var EntityManager 
    */ 
    protected $em; 

    public function init() 
    { 
     $this->add(array(
       'name' => 'title', 
       'attributes' => array(
         'type' => 'text', 
       ), 
       'options' => array(
         'label' => 'Title' 
       ), 
     )); 

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

     $this->add(array(
       'name' => 'content', 
       'attributes' => array(
         'type' => 'textera', 
       ), 
       'options' => array(
         'label' => 'Content' 
       ), 
     )); 

     $this->add(array(
       'name' => 'date', 
       'attributes' => array(
         'type' => 'text', 
         'class' => 'datepicker', 
       ), 
       'options' => array(
         'label' => 'Date', 
       ), 
     )); 


     $this->add(array(
       'name' => 'publication', 
       'attributes' => array(
         'type' => 'Checkbox', 
       ), 
     )); 

     $this->add(array(
       'name' => 'url', 
       'attributes' => array(
         'type' => 'file', 
         'id' => 'files', 
         'class'=> 'upload' 
       ), 
       'options' => array(
         'label' => 'Url' 
       ), 
     )); 

     $this->add(array(
       'name' => 'alt', 
       'attributes' => array(
         'type' => 'text', 
       ), 
       'options' => array(
         'label' => 'Alt' 
       ), 
     )); 

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

     $this->setInputFilter($this->createInputFilter()); 
    } 

    public function __construct($name = null, $options = array()) 
    {   
     parent::__construct($name, $options); 
    } 

    public function createInputFilter() 
    { 
    if (!$this->inputFilter) { 
      $inputFilter = new InputFilter(); 
      $factory  = new InputFactory(); 

      $inputFilter->add($factory->createInput(array(
        'name'  => 'title', 
        'required' => true, 
        'filters' => array(
          array('name' => 'StripTags'), 
          array('name' => 'StringTrim'), 
        ), 
        'validators' => array(
          array(
            'name' => 'StringLength', 
            'options' => array(
              'encoding' => 'UTF-8', 
              'min'  => 6, 
              'max'  => 100, 
            ), 
          ), 
        ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
        'name'  => 'content', 
        'required' => true, 
        'filters' => array(
          array('name' => 'StripTags'), 
          array('name' => 'StringTrim'), 
        ), 
        'validators' => array(
          array(
            'name' => 'StringLength', 
            'options' => array(
              'encoding' => 'UTF-8', 
              'min'  => 10, 
            ), 
          ), 
        ), 
      ))); 

      $inputFilter->add($factory->createInput(array(
        'name'  => 'publication', 
        'required' => false, 
      ))); 

      $inputFilter->add($factory->createInput(array(
        'name'  => 'date', 
        'required' => true, 
      ))); 

      $inputFilter->add($factory->createInput(array(
        'name'  => 'image', 
        'required' => true, 
      ))); 

      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 

    public function setObjectManager(ObjectManager $objectManager) { 
     $this->objectManager = $objectManager; 
    } 

    /** 
    * Get the object manager 
    * 
    * @return ObjectManager 
    */ 
    public function getObjectManager() { 
     return $this->objectManager; 
    } 

} 

내 작업 :

마지막으로
public function addAction() 
{  
    $form = new ArticleForm($this->getObjectManager()); 
    $article = new Article(); 
    $request = $this->getRequest(); 
    $hydrator = new DoctrineHydrator($this->getObjectManager(), get_class($article)); 
    $form->setHydrator($hydrator); 
    $form->bind($article); 
    if ($this->zfcUserAuthentication()->hasIdentity()) { 
     if ($request->isPost()) 
     { 
      $form->setData($request->getPost()); 
      if ($form->isValid()) { 
       $this->getObjectManager()->persist($article); 
        $this->getObjectManager()->flush(); 

        return $this->redirect()->toRoute('blog'); 
      } 
      } 
    } 
    else 
    { 
     return $this->redirect()->toRoute('user'); 
    } 
    return array('form' => $form); 
} 

내가 오류가 있다고 생각 내보기 :이 있다고

<?php 
    $form = $this->form; 
    $form->setAttribute('action', $this->url('add', array('action' => 'add'))); 
    $form->prepare(); 
?> 
<?php 
    echo $this->form()->openTag($form); 
?> 
<ul> 
    <li> 
     <?php echo $this->formHidden($form->get('id'));?> 
    <li> 
    <li> 
     <label>Publication:</label> 
     <?php echo $this->formInput($form->get('publication'));?> 
    </li> 
    <li> 
     <label>Title:</label> 
     <?php echo $this->formInput($form->get('title'));?> 
    </li> 
     // .... 
    <li> 
     <?php echo $this->formSubmit($form->get('submit'));?></li> 
    </ul> 
<?php 
    echo $this->form()->closeTag(); 
?> 

를, 이것이다 거의 모든 코드, 나는 모든 것을 시도했고 어떤 해결책도 찾지 못했습니다. 내 생각에 오류가 있다고 생각합니다. 제발 누군가가 어떤 생각을 가지고 있다면 나는 매우 감사 할 것입니다

답변

0

당신은 init 기능이 있습니다. 이처럼 당신의 __construct 기능을 주석 및 쓰기 : 여기

public function __construct(ObjectManager $em, $name = null, $options = array()) 
{ 
    $this->setObjectManager($em); 
    parent::__construct($name, $options); 

    //here you add all the form elements 
    //(meaning: just put all the content of your init() function here) 
    //if that's a construct function, you also need to add: 
    //private $inputFilter; 
    //at the top of your class, it is not declared in your code 
} 

는 사용자가 제공 한 구조 기능의 : 오류가 발생했다

public function __construct($name = null, $options = array()) 
{ 
    parent::__construct($name, $options); 
} 

, 당신이 쓴 때문에 :

$form = new ArticleForm($this->getObjectManager()); 

그래서를 __construct 함수에 전달한 첫 번째 매개 변수는 objectManager의 인스턴스이고 Zend\View\Helper\Escaper\AbstractHelper에서 처리되었을 때 뭔가 잘못. 무슨 일이 일어나는지 정확히 말할 수는 없지만, 내가 설명한대로 __construct 함수를 선언하면 모든 것이 잘 동작합니다.

2

날짜 개체 때문일 수 있습니다. 는 현재까지 양식 요소의 유형을 변경하려고 :

$this->add(array(
     'name' => 'date', 
     'type' => 'Date', 
     'attributes' => array(
       'type' => 'text', 
       'class' => 'datepicker', 
     ), 
     'options' => array(
       'label' => 'Date', 
     ), 
)); 
+0

u가 양식 요소를 반환하면 \ DateTime 개체의 인스턴스입니까? –

+0

@MarcelDjaman 데이터베이스의 필드 유형이 Date 인 경우보기에서 자동으로 문자열로 변환되고 양식을 저장하면 입력의 문자열이 Date 객체로 변환됩니다. – Sepultura

1

대신 문자열의 속성으로 객체를 설정할 때이 오류를 얻을 수 있습니다 :

$element->setAttribute('class', $object) 

하는 $ 요소가 될 수있다 형식, 필드 집합 또는 요소

1

일반적으로 Object provided to Escape helper, but flags do not allow recursion은 이스케이프 뷰 도우미가 __toString() 메서드가있는 스칼라 또는 개체를 예상했지만 가져 오지 못했음을 의미합니다. 그것은 "멋쟁이, 나는 이것을 인쇄 할 수 없다"고 말하는 환상적인 방법입니다.

해결 방법은 폼보기 도우미를 사용하지 않고 직접 렌더링을 수행하거나 양식 요소 값이 echo인지 확인하는 것입니다.

+0

. 그리고 슬프게도 "[..] print _this_"와 같이 메시지가 지나치게 부정확하며 엔티티의 어떤 부분/속성이 문제를 일으키는 지 알지 못합니다 – humanityANDpeace