Search code examples
phpdoctrine-ormzend-framework2zend-db

Zend Framework 2 - Annotation forms and Doctrine2 - Catchable fatal error - ObjectManager


I'm using Zend, Doctrine2.1 and AnnotationForms.

My entity looks like this:

/**
 * @ORM\Entity
 * @ORM\Table(name="myentity")
 * @Form\Name("myentity")
 * @Form\Attributes({ "class": "form-horizontal" })
 * @Form\Hydrator("\DoctrineModule\Stdlib\Hydrator\DoctrineObject")
*/
class MyEntity {
   ...
}

When using this DoctrineObject I get the following error:

Catchable fatal error: Argument 1 passed to DoctrineModule\Stdlib\Hydrator\DoctrineObject::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, none given, called in C:\vendor\zendframework\zendframework\library\Zend\Form\Factory.php on line 566 and defined in C:\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php on line 63

I cannot use Zend\Stdlib\Hydrator\ObjectProperty because then I get

Fatal error: Cannot access protected property

I'm quite lost. Anybody an idea what I can do to fix this issue?

I'm guessing that I need a __construct() function. But what do I put in there?


Solution

  • Someone may come up with a Annotation-only function, if that exists, meanwhile you can go this approach:

    $form = //create the annotation form WITHOUT a hydrator
    $objectManager = $serviceLocator->get('Doctrine\ORM\EntityManager');
    $hydrator = new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager);
    $form->setHydrator($hydrator);
    
    //continue in your controller