【问题标题】:Object provided to Escape helper, but flags do not allow recursion in Zend Framework 2提供给 Escape 助手的对象,但标志不允许 Zend Framework 2 中的递归
【发布时间】:2014-02-27 21:23:59
【问题描述】:

所以我在一个 Zend Framework 项目中工作并且我正在使用 Doctrine,我创建了我的表单、控制器和实体,但是当我运行我的项目时出现了这个错误:

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();
?>

就是这样,这几乎是我的代码,我尝试了一切,但没有找到任何解决方案,我认为我认为错误,所以如果有人有任何想法,我将非常感激

【问题讨论】:

    标签: php zend-framework doctrine-orm zend-framework2 zend-form


    【解决方案1】:

    它可能是因为日期对象。 尝试将表单元素的类型更改为日期:

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

    【讨论】:

    • 当你返回表单元素时,它是 \DateTime 对象的实例吗?
    • @MarcelDjaman 如果数据库中的字段是 Date 类型,那么它会在您的视图中自动转换为字符串,如果您保存表单,则输入中的字符串会转换为 Date 对象.
    【解决方案2】:

    一般来说,Object provided to Escape helper, but flags do not allow recursion 表示 Escape 视图助手期待,但没有得到一个标量或带有 __toString() 方法的对象。这是说“老兄,我不能打印这个”的一种奇特方式。

    解决方案是自己进行渲染(不使用表单视图助手),或者确保表单元素值是您可以echo 的值。

    【讨论】:

    • 完全正确。可悲的是,就像“[..] print this”一样,消息过于不精确,它没有说明实体的哪个部分/属性导致了问题
    【解决方案3】:

    将对象设置为属性而不是字符串时会出现此错误:

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

    其中 $element 可以是表单、字段集或元素

    【讨论】:

      【解决方案4】:

      你有一个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 函数,一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 1970-01-01
        相关资源
        最近更新 更多