I working with zf2.1.3 and doctrine 2. I was trying to hydrate information on my class and realize that the DoctrineModule\Stdlib\Hydrator\DoctrineObject
doesn't work with fields that have underline on it, like cat_id
.
Here an example:
/* namespace Application\Entity; */
class Foo
{
private $cat_id;
private $cat_name;
public function getCatId()
{
return $this->cat_id;
}
public function setCatName($name)
{
$this->cat_name = $name;
return $this;
}
public function getCatName()
{
return $this->cat_nome;
}
}
class Bar
{
private $id;
private $name;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->nome;
}
}
/* namespace Application\Controller; */
use \DoctrineModule\Stdlib\Hydrator\DoctrineObject;
public function indexAction()
{
$hydrator = new DoctrineObject($this->getEntityManager(), 'Application\Entity\Foo');
$foo = $hydrator->hydrate(array('cat_name' => 'Frank Moraes'), new Foo());
\Zend\Debug\Debug::dump($foo, 'Foo Hydrator');
$hydrator = new DoctrineObject($this->getEntityManager(), 'Application\Entity\Bar');
$bar = $hydrator->hydrate(array('name' => 'Frank Moraes'), new Bar());
\Zend\Debug\Debug::dump($inscrit, 'Bar Hydrator');
}
This code returns the following:
Foo Hydrator
object(Application\Entity\Foo)
private 'cat_id' => null
private 'cat_name' => null
Bar Hydrator
object(Application\Entity\Foo)
private 'id' => null
private 'name' => 'Frank Moraes'
So my question is: Why Doctrine Hydrator doesn't work with fields that have underline in it? How can I make this work?
Thank you!
Edited
Sorry for the long time with no answer. A have no access to SO on my work!
I tried the following:
$hydrator = new DoctrineObject($this->getEntityManager(), 'Application\Entity\Foo', false);
For the example I posted here, this false
parameter works fine.
But, it didn't work when I'm binding the class on a form!
Someone have a clue?
Several months after this has been asked, but I've been checking the source code of the DoctrineObject
hydrator just now, and I think this is what's going on:
By default, unless you construct the DoctrineObject
hydrator with the byValue
flag as false
, the hydrator will work in byValue
mode. What that means is that it tries to construct getter and setter method names from the values that you're trying to hydrate, and the way it does that is by calling ucfirst
on the field name and prepending get/set to that.
So, for instance, you have cat_name
, so it will try the getter method getCat_name
which clearly is incorrect.
You have 4 choices, then:
camelCase
your variable namesbyValue
to false (so that it tries to access the variables directly) [although I think you might have to make the variables public in that case... I'm not sure how visibility will affect it, as I haven't tried it before]Strategy
orgetCat_name
(please don't do this).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With