doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.92k stars 2.51k forks source link

Hydrator giving wrong entity #5852

Open AntonioCS opened 8 years ago

AntonioCS commented 8 years ago

Hey,

I am working on a legacy symfony2 project with doctrine2 (components at the bottom) I had to create a simple cache system using a table.

The table has the following data:

id;key;value;is_serialized;created;ttl
1;test;1111111111111111111;0;2016-06-03 22:46:58;\N
2;aaaaatest2;22222222222222222222;0;2016-06-03 22:46:58;\N
3;1212aaaaa;3333333333333333333;0;2016-06-03 22:46:58;\N
4;dd23fdsdaaaaa;4444444444444444444;0;2016-06-03 22:46:58;\N

I have the following code to retrieve data (this is an internal method used by the public method fetch):

  private function fetchEntity($key)
        {
            /* @var $res Storage */
            $res = $this->getEntityManager()
                ->getRepository(Storage::class)
                ->findOneBy(['key' => $key]);

            if ($res) {
                return $res;
            }

            return null;
        }

The problem I have is that given the following code:

       var_dump($storage->fetch('aaaaatest2'));
        var_dump($storage->fetch('1212aaaaa'));
        var_dump($storage->fetch('dd23fdsdaaaaa'));
        var_dump($storage->fetch('11dd23fdsdaaaaa'));

I will ALWAYS get the value of the first fetch. In this case the value is 22222222222222222222 for all the calls.

I can see the query being formed in mysql log and the query is correct. If I grab the query from the log and run it in mysql I get the value I want.

From what I can see (using a debugger) is that on file vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 699 I get the wrong value. The code is:

 $hydrator = $this->_em->newHydrator($this->_selectJoinSql ? Query::HYDRATE_OBJECT : Query::HYDRATE_SIMPLEOBJECT);
$entities = $hydrator->hydrateAll($stmt, $this->_rsm, $hints);

What am I doing wrong here????

Here are my doctrine2 configs:


doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        types:
            json: Sonata\Doctrine\Types\JsonType
            point: PitchBundle\ORM\PointType
        mapping_types:
            point: string
            enum: string

    orm:
        entity_managers:
            default:
                auto_mapping: true
                dql:
                    numeric_functions:
                        point_str: PitchBundle\ORM\PointStr
                        distance: PitchBundle\ORM\Distance
        auto_generate_proxy_classes: %kernel.debug%

There are the doctrine components I am using:

doctrine/annotations                     v1.1.2 
doctrine/cache                           v1.2.0 
doctrine/collections                     dev-master bcb5377 
doctrine/common                          2.4.x-dev c94d6ff 
doctrine/data-fixtures                   dev-master 8ffac1c 
doctrine/dbal                            2.3.x-dev 59c310b 
doctrine/doctrine-bundle                 dev-master a41322d
doctrine/doctrine-fixtures-bundle        dev-master 3caec48
doctrine/doctrine-migrations-bundle      dev-master 1a7f58d 
doctrine/inflector                       dev-master 8b4b3cc
doctrine/lexer                           dev-master bc0e1f0
doctrine/migrations                      dev-master e960224 
doctrine/orm                             2.3.x-dev 66d8b43
TomasVotruba commented 8 years ago

Not sure if it matters, but method name is fetchEntity() and in the example code you use fetch().

Ocramius commented 8 years ago

@TomasVotruba marking it as "missing tests" - can't reproduce it with this info, sorry.

Ocramius commented 8 years ago

Also, doctrine/orm 2.3.x is unmaintained: please upgrade