Doctrine 2 Hydrator - Underline Fields

I work with zf2.1.3 and doctrine 2. I tried to remove information about my class and understand that it DoctrineModule\Stdlib\Hydrator\DoctrineObjectdoes not work with fields that emphasize it, for example cat_id.

Here is 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 does Doctrine Hydrator not work with fields that underline it? How can I do this job?

Thank!

Edited

Sorry for a long time without an answer. I do not have access to SO at my work!

I tried the following:

$hydrator = new DoctrineObject($this->getEntityManager(), 'Application\Entity\Foo', false);

In the example here, this option falseworks fine.

But it didn’t work when I attach the class to the form!

Does anyone have a key?

+5
source share
3 answers

, DoctrineObject , , :

, DoctrineObject byValue false, byValue. , getter setter , , , , ucfirst get/set .

, cat_name, getter getCat_name, .

4 , :

  • A: camelCase
  • B: byValue false ( ) [ , , , ... , , ]
  • C: Strategy
  • D: , getCat_name ( ).
+9

...

foreach ($form as $key => $value) {
        while ($pos = strrpos($key,'_')) {                                  
            $key = substr_replace($key, '', $pos, 1);
            $capitalized = strtoupper($key[$pos]);
            $key[$pos] = $capitalized;                                
        }
        $data[$key] = $value;
    }
0

:

/** @ORM\Column(name="column_name", type="string") */
protected $columnName;

function get(...);
function set($columnName){$this->columnName = $columnName}

0

All Articles