2
votes

I am having problems getting my doctrine hydrator to hydrate my return post forms.

Post forms.

I keep getting the following message:

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

but this cannot be correct because I have a validator on my form requiring this value to be inserted, yet my form is validating.

I would really appreciate any help or advice on resolving the problem or advice on how to go about discovering what is causing the problem.

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

these are the var_dumped values from the returned form:

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)

As you can see, the values are clearly there, which means that the problem might be in the hydrators.

I now enclosed the rest of the documents.

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

    } 

The fieldset

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',
            ),
        ));

}

** The Form**

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

Here is the var_daump of the persist in the controller:

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



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

You will note that they are empty, yet the var dump of the values from the returned post clearly contain the values.

I enclose my workstatement class. you will note that I have used the magic 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
            )
        );
    }


}  
2
Please paste your entity. Maybe you forgot about the title getter. - Adrian Nowicki

2 Answers

2
votes

DoctrineHydrator by default is hydrating and extracting values using getters and setters. If your entity doesn't have these methods then it cannot work properly. If you dont' want to use getters/setters, use new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement', false) instead of new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement').

Maybe it's not the reason why hydrator doesn't work. Please edit your first post and paste Workers\Entity\WorkerStatement class.

EDIT

Hydrator is calling getTitle() and your magic method is trying to access getTitle property which doesn't exist. You have three options:

  1. Change DoctrineHydrator to new DoctrineHydrator($objectManager, 'Workers\Entity\WorkerStatement', false).
  2. Add getters and setters. For example getTitle(), setTitle($title).
  3. Refactor magic methods to accept getProperty, setProperty.
0
votes

Actually You dont need to add the hydrator in the form , use it in the controller (or service) if its necessary .

plz add a var dump before :

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

and post the result