Zend Framework 2 - Hydrator strategy not responding and hydrating
Asked Answered
M

2

0

I implemented basically this strategy.

The main difference is (I guess) that I use Doctrine2.

The constructor class is called (a test echo is printed) but the two functions extract() and hydrate() are not.

I added the strategy as follows:

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

A kind-of similar issue was posted here.

Maybe the problem is in the way how I add this strategy. But I honestly don't know...

Would be great if somebody could give me a hint what I'm doing wrong.

Muraida answered 3/1, 2013 at 14:5 Comment(1)
First check that the 'my_attribute' name. It may be using camel case within that particular hydrator but outputting lowercase underscore. One way of doing this would be to get the class name of the hydrator and checking the hydrate() method. You should see a call to hydrateValue() - the first parameter to this method is the name of the field - so I would echo these out. As I'm not familiar with Doctrine in ZF2 - if you post the Hydrator class I'll try and assist further.Heaney
M
1

Please refer to this post, for a cleaner and more appropriate solution!

Zend Framework 2 - Hydrator strategy for Doctrine relationship not working

Marriageable answered 3/1, 2013 at 19:20 Comment(0)
M
0

@Sam solved this problem on GitHub.

This is a problem that is commonly happening currently. The thing is: addStrategy() is a thing of ZF2's ClassMethodHydrator - this hydrator is used silently within DoctrineEntity. To add custom Strategies, simply do not use DoctrineEntity but use ClassMethodsHydrator itself. On ClassMethods then you'll be able to add Strategies

You may also take a look at this Issue/PR doctrine/DoctrineModule#106

My code now looks like this:

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

BTW: The underscore in the attribute doesn't cause any problems

Unfortunately I stumbled upon another foreign-key-relation-based issue, it seems even a Doctrine issue. But I'll open a new question for this.

Muraida answered 3/1, 2013 at 15:35 Comment(1)
Granted, my explanation is horribly wrong when looking at the code of the hydrators, but the solution gets it working after all.Marriageable

© 2022 - 2024 — McMap. All rights reserved.