zend 2 + doctrine 2 hydrator - 保湿剂不会给POST物体加水

时间:2013-10-01 23:12:44

标签: forms doctrine-orm zend-framework2

我在使用我的学说保湿剂来补充我的回归表格时遇到了问题。

发布表格。

我不断收到以下消息:

  

执行'INSERT INTO worker_essay时发生异常   (标题)VALUES(?)'with params [null]:SQLSTATE [23000]:完整性   约束违规:1048列'title'不能为空

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

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

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)

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

我现在附上其余的文件。

控制器

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

以下是控制器中持久性的var_daump

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



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

您将注意到它们是空的,但返回的帖子中值的var转储显然包含值。

我附上我的工作阶段课程。你会注意到我使用了魔法吸气剂/二传手。

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


}  

2 个答案:

答案 0 :(得分:2)

默认情况下,

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。

答案 1 :(得分:0)

实际上你不需要在表格中添加保湿剂,如果需要,可以在控制器(或服务)中使用。

plz之前添加var转储:

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

并发布结果