1
votes

I am trying to implement doctrine hydrator in my zend 2 project. I am using doctrine's official documentation

I am getting two warning and one error. Following is the warning at top of page.

Warning: Missing argument 2 for DoctrineModule\Stdlib\Hydrator\DoctrineObject::__construct(), called in /path/to/my/project/module/Notes/src/Notes/Form/AssignForm.php on line 16 and defined in /path/to/my/project/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php on line 71

Notice: Undefined variable: targetClass in /path/to/my/project/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php on line 76

Here is the error:

An error occurred An error occurred during execution; please try again later. Additional information: Doctrine\Common\Persistence\Mapping\MappingException

File:

/path/to/my/project/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96

Message:

Class '' does not exist

Here is my entity:

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Assigned{

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

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

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

/** @ORM\Column(type="datetime") 
* @access protected
*/
protected $hold_date;

/** @ORM\Column(type="datetime") 
* @access protected
*/
protected $vsi_date;

/** @ORM\Column(type="integer") 
* @access protected
*/
protected $hold_status;

/** @ORM\Column(type="integer") 
* @access protected
*/
protected $vsi_status;

/**
* @param string $id
* @return Assign
*/

 // id should be auto incremental in database.
 /*
 public function setId($id)
 {
     $this->id = $id;
     return $this;
 }
*/
 /**
  * @return string $id;
  */
 public function getId()
 {
     return $this->id;
 }

 /**
  * @param string $loan_number
  * @access public
  * @return Assigned      
  */
 public function setLoanNumber($loan_number)
 {
     $this->loan_number = $loan_number;
     return $this;
 }

 /**
  * @return string $loan_number
  */
 public function getLoanNumber()
 {
     return $this->loan_number;
 }

 /**
  * @param string $claim_status
  * @access public
  * @return Assigned      
  */
 public function setClaimStatus($claim_status)
 {
     $this->claim_status = $claim_status;
     return $this;
 }

 /**
  * @return string $claim_status;
  */
 public function getClaimStatus()
 {
     return $this->claim_status;
 }

 /**
  * @param datetime $hold_date
  * @access public
  * @return Assigned
  */
 public function setHoldDate($hold_date)
 {
     $this->hold_date = new \DateTime($hold_date);
     return $this;
 }

 /**
  * @return datetime $hold_date;
  */
 public function getHoldDate()
 {
     return $this->hold_date;
 }

 /**
  * @param datetime $vsi_date
  * @access public
  * @return Assigned      
  */
 public function setVsiDate($vsi_date)
 {
     $this->vsi_date = new \DateTime($vsi_date);
     return $this;
 }

 /**
  * @return datetime $vsi_date;
  */
 public function getVsiDate()
 {
     return $this->vsi_date;
 }

 /**
  * @param integer $hold_status
  * @access public
  * @return Assigned      
  */
 public function setHoldStatus($hold_status)
 {
     $this->hold_status = $hold_status;
     return $this;
 }

 /**
  * @return integer $hold_status;
  */
 public function getHoldStatus($hold_status)
 {
     return $this->hold_status;
 }

 /**
  * @param integer $vsi_status
  * @access public
  * @return Assigned      
  */
 public function setVsiStatus($vsi_status)
 {
     $this->vsi_status = $vsi_status;
     return $this;
 }

 /**
  * @return integer $vsi_status;
  */
 public function getVsiStatus()
 {
     return $this->vsi_status;
 }
}

Here is my Controller

<?php
namespace Notes\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Doctrine\ORM\EntityManager; 
use Application\Entity\Assigned;
use Notes\Form\AssignForm;
use Notes\Form\NotesFieldset;

class NotesController extends AbstractActionController
{

protected $objectManager;

public function indexAction()
{

     return new ViewModel();
}

public function addAction()
{
    // Get your ObjectManager from the ServiceManager
$objectManager = $this->getOBjectManager();
$form = new AssignForm($objectManager);

// Create a new, empty entity and bind it to the form
$assigned = new Assigned();
$form->bind($assigned);

if ($this->request->isPost()) {
    $form->setData($this->request->getPost());

    if ($form->isValid()) {
        $objectManager->persist($assigned);
        $objectManager->flush();
    }
}

return array('form' => $form);


}

public function getOBjectManager(){
    if(!$this->objectManager){
        $this->objectManager = $this->getServiceLocator()
                                ->get('Doctrine\ORM\EntityManager');
    }
    return $this->objectManager;
}

}

Here is my form class:

<?php
namespace Notes\Form;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
use Notes\Form\NotesFieldset;

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

    // The form will hydrate an object of type "BlogPost"
    $this->setHydrator(new DoctrineHydrator($objectManager));

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

    // … add CSRF and submit elements …

    // Optionally set your validation group here

}
}

Here is the Fieldset class

<?php
namespace Notes\Form;
use Application\Entity\Assigned;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class NotesFieldset extends Fieldset implements InputFilterProviderInterface
{
    protected $inputFilter;

   public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('assigned');

        $this->setHydrator(new DoctrineHydrator($objectManager))
             ->setObject(new Assigned());

        $this->add(array(
            'type' => 'Zend\Form\Element\Hidden',
            'name' => 'id'
        ));

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

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

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

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

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

        $this->add(array(
            'name' => 'vsi_status',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'VSI Status',
            ),
        ));

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

   /**
   * Define InputFilterSpecifications
   *
   * @access public
   * @return array
   */

    public function getInputFilterSpecification()
    {

        return array(
            'id' => array(
                'required' => false
            ),
            'name' => array(
                'required' => true
            ),
            'loan_number' => array(
                'required' => true
            ),
            'claim_status' => array(
                'required' => true
            ),
            'hold_date' => array(
                'required' => true
            ),
            'hold_status' => array(
                'required' => true
            ),
            'vsi_date' => array(
                'required' => true
            ),
            'hold_status' => array(
                'required' => true
            ),
        );

    }
}

It is saying : Class '' does not exist. But the class name is not given in the message.

Beside this I am using these library for doctrine in my composer.

"doctrine/doctrine-orm-module": "0.7.*",
"doctrine/migrations": "dev-master",
"doctrine/common": "2.4.*@dev",
"doctrine/annotations": "1.0.*@dev",
"doctrine/data-fixtures": "1.0.*@dev",
"doctrine/cache": "1.0.*@dev",
"zf-commons/zfc-user-doctrine-orm": "dev-master",

Please tell me what is missing in my implementation. I have got one open issue in github for doctrine with incomplete example docs. Here is link of issue. If this is the case, then please suggest me ho to implement correctly.

1
Correct way is pointed out in Answer by @AlexP The mistake you did was to use an the documentation of version 0.7 of the Hydrator. If you check the 0.8 or dev-master branch, you'd notice the changes ;) Always read documentation in the version you're downloading the sources for ;)Sam
@sam As you see in my composer list above there is "doctrine/doctrine-orm-module": "0.7.*" . This means I am using doctrine module of version 0.7.x, right? So how could it be different? How do you say that I am using different version's documentation? I have used 0.7.x library and trying to use documentation version 0.7.x (github.com/doctrine/DoctrineModule/blob/0.7.x-dev/docs/…). Can you make it more clear? Thanksregeint

1 Answers

3
votes

Your missing the target class parameter:

$this->setHydrator(new DoctrineHydrator($objectManager));

Should be:

$this->setHydrator(
  new DoctrineHydrator(
    $objectManager,
    'the\target\entity' // <-- the target entity class name
  )
);

The targetClass is used within the DoctrineObject to fetch the metadata of the hydrated entity. You can see this in DoctrineModule\Stdlib\Hydrator\DoctrineObject::__construct():

$this->metadata = $objectManager->getClassMetadata($targetClass);