zend2 doctrine 2 form intgergration OneToOne

2019-09-15 04:00发布

问题:

I am trying to integrate Doctrine 2 to my Zend 2. I wish to join two entities on my form; i.e ONeToOne.

I followed the tutorial by gammamatrix.

I was able to successfully integrate doctrine and was able to Hydrate one of the entities.

The problem occurs when I try to join the second entity fieldset to the first entity. I keep getting the following fatal error message:

**Catchable fatal error: Argument 1 passed to Workers\Form\Fieldset\WorkerAddressFieldset::__construct() must implement interface Doctrine\Common\Persistence\ObjectManager, none given,**

I will start with the fieldsets and show you how I tried to pass the ObjectManager to both fieldsets.

The first fieldset: AboutYou (This is the owner in the relationship);

I pass the ObjectManager through the form on the controller page:

$form = new CreateAboutYouForm($this->getEntityManager());

The first part of the below code works alone. i.e if i dont place the workeraddressfieldset into the code.

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

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

$this->add(array(
            'name' => 'firstName',
            'type' => 'Text',
            'options' => array(
                'label' => 'First Name',
            ),
        ));

$addressFieldset = new WorkerAddressFieldset($objectManager);
         $this->add(array(
            'type' => 'Workers\Form\Fieldset\workerAddressFieldset',
            'name' => 'WorkerAddress',
            'options' => array(
                'label' =>  $addressFieldset
            )
        ));

}

You will notice above that I created a workerAddressFieldset object and attempted to pass the ObjectManager into the class that way. But it does not seem to receive the values. I am not clear why.

The second fieldset (the workersAddress):

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

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

}

I would really appreciate some advice or a working sample of how others have been able to join the fieldsets together.

回答1:

That's because you try to add the Fieldset from String. Given the fact that your AddressFieldset has a __construct(ObjectManager $om) however you first need to instantiate the Fieldset and then add it to your form. Like:

class AboutYouFieldset extends Fieldset 
{
    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('about-you');

        $this->add(); // your first element

        $fieldset = new WorkerAddressFieldset($objectManager);
        $this->add($fieldset);
    }
}