forms - zend 2 + 学说 2 Hydrator - Hydrator 未对 POST 对象进行水合

标签 forms doctrine-orm zend-framework2

我在使用学说水合器来水合我的返回表格时遇到问题。

发布表格。

我不断收到以下消息:

An exception occurred while executing 'INSERT INTO worker_essay (title) VALUES (?)' with params [null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null

但这不可能是正确的,因为我的表单上有一个验证器要求插入此值,但我的表单正在验证。

我非常感谢任何关于解决问题的帮助或建议,或者关于如何发现问题原因的建议。

public function getInputFilterSpecification()
  {
        return array(
            'title' => array(
                'required' => true
            ),
        );
    }

这些是返回表单中的 var_dumped 值:

object(Zend\Stdlib\Parameters)[146]   public 'WorkerStatement' => 
     array (size=2)
       'id' => string '' (length=0)
       'title' => string 'the values from title' (length=21)   public 'submit' => string 'Submit' (length=6)

正如您所看到的,这些值明显存在,这意味着问题可能出在水合器中。

我现在附上其余的文件。

Controller

public function workerStatementAction()
    {
         $form = new CreateWorkerStatementForm($this->getEntityManager());

         $workerStatement = new WorkerStatement();
      //  $form->setInputFilter($workerEssay->getInputFilter());
         $form->bind($workerStatement);
       //    var_dump($workerStatement); die();
          if ($this->request->isPost()) {
            $post  = $this->request->getPost();
            $form  =  $form->setData($this->request->getPost());
             if ($form->isValid()) {
                $post =$this->request->getPost();
                  $this->getEntityManager()->persist($workerStatement);
                 $this->getEntityManager()->flush();
             // Redirect to list of aboutyou
             return $this->redirect()->toRoute('worker');    
             }
         }
         return array('form' => $form);

    } 

字段集

class WorkerStatementFieldset extends Fieldset implements InputFilterProviderInterface
{
   public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('WorkerStatement');


        $this->setHydrator(new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement'))
             ->setObject(new WorkerStatement());


  $this->add(array(
            'name' => 'title',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => 'title',
            ),
        ));

}

** 表格**

class CreateWorkerStatementForm extends Form
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('WorkerStatement');

        // The form will hydrate an object of type "AboutYou"
        $this->setHydrator(new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement'));

        // Add the user fieldset, and set it as the base fieldset
      $workerStatementFieldset = new WorkerStatementFieldset($objectManager);
      $workerStatementFieldset->setUseAsBaseFieldset(true);
      $this->add($workerStatementFieldset);
 }
}

这是 Controller 中持久化的var_daump:

 $this->getEntityManager()->persist($workerStatement);



object(Workers\Entity\WorkerStatement)[351]
  protected 'id' => null
  protected 'title' => null

您会注意到它们是空的,但返回帖子中的值的 var 转储清楚地包含这些值。

附上我的工作陈述类(class)。你会注意到我使用了魔法 getter/setter。

<?php

namespace Workers\Entity;

use Doctrine\ORM\Mapping as ORM;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface; 

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
 *
 * @ORM\Entity
 * @ORM\Table(name="worker_essay")
 * @property string $title
 */
class WorkerStatement   
{


    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;


     /**
     * @ORM\Column(type="string")
     */
    protected $title;

    /**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
return $this->$property;
}

/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$this->$property = $value;
}




     public function getInputFilterSpecification()
    {
        return array(

            'title' => array(
                'required' => true
            )
        );
    }


}  

最佳答案

DoctrineHydrator默认情况下,使用 getter 和 setter 来合并和提取值。如果您的实体没有这些方法,那么它就无法正常工作。如果您不想使用 getter/setter,请使用 new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement', false)而不是new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement') .

也许这不是保湿器不起作用的原因。请编辑您的第一篇文章并粘贴 Workers\Entity\WorkerStatement类。

编辑

Hydrator 正在调用 getTitle() 并且您的魔术方法正在尝试访问不存在的 getTitle 属性。您有三个选择:

  1. 将 DoctrineHydrator 更改为 new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement', false) .
  2. 添加 getter 和 setter。例如getTitle() , setTitle($title) .
  3. 重构魔术方法以接受 getProperty、setProperty。

关于forms - zend 2 + 学说 2 Hydrator - Hydrator 未对 POST 对象进行水合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127537/

相关文章:

php - Doctrine 2 需要 Symfony 吗?

zend-framework2 - zend 框架 2 服务管理器可调用和工厂之间的区别

php - php中获取具有相同id的字段的最大值

php - symfony 4 多种形式循环 Twig 提交

php - 通过动态php下拉列表从mysql获取字段值并将该值导出到word文件

symfony - Doctrine 实体管理器实例

zend-framework2 - 使用 ZF2 嵌套选择

forms - Symfony2 表格 : Generating checkboxes

php - 表单生成器中的关联数组字段 - Symfony3

join - 原则 2 JOIN ON 错误