Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 Hydrator - fields with underline

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?

like image 274
Vinicius Garcia Avatar asked Mar 11 '13 01:03

Vinicius Garcia


1 Answers

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:

  • A: camelCase your variable names
  • B: Set byValue 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]
  • C: Use a different hydration Strategy or
  • D: Just have weird getter and setter names like getCat_name (please don't do this).
like image 159
osdiab Avatar answered Oct 16 '22 07:10

osdiab