Zend Framework 2 - Hydrator strategy for Doctrine relationship not working
Asked Answered
P

1

1

As mentioned here I'm building a custom hydration strategy to handle my related objects in a select box in a form.

My form looks like this:

$builder = new AnnotationBuilder($entityManager);
$form    = $builder->createForm(new MyEntity());
$form->add(new MyFieldSet());

$hydrator = new ClassMethodsHydrator();
$hydrator->addStrategy('my_attribute', new MyHydrationStrategy());
$form->setHydrator($hydrator);

$form->get('my_attribute')->setValueOptions(
      $entityManager->getRepository('SecEntity\Entity\SecEntity')->fetchAllAsArray()
);

When I add a new MyEntity via the addAction everything works great.

I wrote fetchAllAsArray() to populate my selectbox. It lives within my SecEntityRepository:

public function fetchAllAsArray() {

    $objects = $this->createQueryBuilder('s')
        ->add('select', 's.id, s.name')
        ->add('orderBy', 's.name ASC')
        ->getQuery()
        ->getResult();

    $list = array();
    foreach($objects as $obj) {
        $list[$obj['id']] = $obj['name'];
    }

    return $list;
}

But in the edit-case the extract() function doesn't work. I'm not at the point where I see something of hydrate() so I'll leave it out for now.

My hydrator strategy looks like this:

class MyHydrationStrategy extends DefaultStrategy
{
    public function extract($value) {        
        print_r($value);
        $result = array();
        foreach ($value as $instance) {
            print_r($instance);
            $result[] = $instance->getId();
        }
        return $result;
    }

    public function hydrate($value) {
        ...
    }

The problem is as follows:

Fatal error: Call to a member function getId() on a non-object

The print_r($value) returns loads of stuff beginning with

DoctrineORMModule\Proxy__CG__\SecEntity\Entity\SecEntity Object

following with something about BasicEntityPersister and somewhere in the mess are my referenced entities.

The print_r($instance) prints nothing. It's just empty. Therefore I guess is the error message legit... but why can't I iterate over these objects?

Any ideas?

Edit:

Regarding to @Sam:

My attribute in the entity:

    /**
 * @ORM\ManyToOne(targetEntity="Path/To/Entity", inversedBy="whatever")
 * @ORM\JoinColumn(name="attribute_id", referencedColumnName="id")
 * @Form\Attributes({"type":"hidden"})
 *
 */
protected $attribute;

My new selectbox:

$form->add(array(
        'name'       => 'attribute',
        'type'       => 'DoctrineModule\Form\Element\ObjectSelect',
        'attributes' => array(
            'required' => true
        ),
        'options'    => array(
            'label'           => 'MyLabel',
            'object_manager'  => $entityManager,
            'target_class'    => 'Path/To/Entity',
            'property'        => 'name'
        )
    ));

My final hope is that I'm doing something wrong within the controller. Neither my selectbox is preselected nor the value is saved...

...

$obj= $this->getEntityManager()->find('Path/To/Entity', $id);

    $builder = new \MyEnity\MyFormBuilder();
    $form = $builder->newForm($this->getEntityManager());

    $form->setBindOnValidate(false);
    $form->bind($obj);
    $form->setData($obj->getArrayCopy());

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

        if ($form->isValid()) {
            $form->bindValues();
            $this->getEntityManager()->flush();

            return $this->redirect()->toRoute('entity');
        }
    }
Pretender answered 3/1, 2013 at 16:0 Comment(2)
Question: Why do you not use the Doctrine Form Elements?Hemminger
Good one :) I'm not aware of their existence I have to admit... do you know a good tutorial for them? if they work it's a legit answer for this question I'd say ;)Pretender
H
12

I still haven't come around to write the tutorial for that :S

I don't know if this is working with the annotationbuilder though! As the DoctrineModule\Form\Element\ObjectSelect needs the EntityManager to work. The options for the ObjectSelect are as follows:

   $this->add(array(
        'name'       => 'formElementName',
        'type'       => 'DoctrineModule\Form\Element\ObjectSelect',
        'attributes' => array(
            'required' => true
        ),
        'options'    => array(
            'label'           => 'formElementLabel',
            'empty_option'    => '--- choose formElementName ---',
            'object_manager'  => $this->getEntityManager(),
            'target_class'    => 'Mynamespace\Entity\Entityname',
            'property'        => 'nameOfEntityPropertyAsSelect'
        )
    ));

In this case i make use of $this->getEntityManager(). I set up this dependency when calling the form from the ServiceManager. Personally i always do this from FactoryClasses. My FormFactory looks like this:

public function createService(ServiceLocatorInterface $serviceLocator)
{
    $em = $serviceLocator->get('Doctrine\ORM\EntityManager');

    $form = new ErgebnishaushaltProduktForm('ergebnisform', array(
        'entity_manager' => $em
    ));

    $classMethodsHydrator = new ClassMethodsHydrator(false);

    // Wir fügen zwei Strategien, um benutzerdefinierte Logik während Extrakt auszuführen
    $classMethodsHydrator->addStrategy('produktBereich', new Strategy\ProduktbereichStrategy())
                         ->addStrategy('produktGruppe', new Strategy\ProduktgruppeStrategy());

    $hydrator = new DoctrineEntity($em, $classMethodsHydrator);

    $form->setHydrator($hydrator)
         ->setObject(new ErgebnishaushaltProdukt())
         ->setInputFilter(new ErgebnishaushaltProduktFilter())
         ->setAttribute('method', 'post');

    return $form;
}

And this is where all the magic is happening. Magic, that is also relevant to your other Thread here on SO. First, i grab the EntityManager. Then i create my form, and inject the dependency for the EntityManager. I do this using my own Form, you may write and use a Setter-Function to inject the EntityManager.

Next i create a ClassMethodsHydrator and add two HydrationStrategies to it. Personally i need to apply those strategies for each ObjectSelect-Element. You may not have to do this on your side. Try to see if it is working without it first!

After that, i create the DoctrineEntity-Hydrator, inject the EntityManager as well as my custom ClassMethodsHydrator. This way the Strategies will be added easily.

The rest should be quite self-explanatory (despite the german classnames :D)

Why the need for strategies

Imo, this is something missing from the DoctrineEntity currently, but things are still in an early stage. And once DoctrineModule-Issue#106 will be live, things will change again, probably making it more comfortable.

A Strategy looks like this:

<?php
namespace Haushaltportal\Stdlib\Hydrator\Strategy;

use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;

class ProduktbereichStrategy implements StrategyInterface
{
    public function extract($value)
    {
        if (is_numeric($value) || $value === null) {
            return $value;
        }

        return $value->getId();
    }

    public function hydrate($value)
    {
        return $value;
    }
}

So whenever the $value is not numeric or null, meaning: it should be an Object, we will call the getId() function. Personally i think it's a good idea to give each Element it's own strategy, but if you are sure you won't be needing to change the strategy at a later point, you could create a global Strategy for several elements like DefaultGetIdStrategy or something.

All this is basically the good work of Michael Gallego aka Bakura! In case you drop by the IRC, just hug him once ;)

Edit An additional resource with a look into the future - updated hydrator-docs for a very likely, soon to be included, pull request

Hemminger answered 3/1, 2013 at 19:19 Comment(5)
even without the strategy no error occurs and everything looks fine but in the edit case it wont save/overwrite the foreign key. even if the correct one is in the POST AND in the form after binding! adding your strategy doesnt change it... ideas?Pretender
Do you include the ID in your form? Will be hard to tell without the whole code otherwise.Hemminger
oddest thing ever! I altered my controller and set the formElement value manually to my id and after bindValues() also manually to my object again. it works...! very ugly but until then I'm unfortunately forced to stick to this solution.Pretender
Sry for my late reply, fell asleep :D bindValues(), you fetch the object from the DB, like $ent = $repo->find(1) and then bind it to the form via $form->bind($ent). Using this all getter function will be called, populating the form with identical element names.Hemminger
so i'd need to alter my getter/setter for the FKs to handle id vs object?Pretender

© 2022 - 2024 — McMap. All rights reserved.