[DDC-2130] A problem with obtaining Entity without cache Created: 07/Nov/12  Updated: 07/Nov/12  Resolved: 07/Nov/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.1
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: SergSW Assignee: Benjamin Eberlei
Resolution: Invalid Votes: 0
Labels: None
Environment:

Symfony 2.0.18


Attachments: File SimpleEntity.php     File SimpleEntityTest.php    

 Description   

I use a custom validation constraint.
In the constraint I want to compare old and new entity, but when I take a entity from database, I got a entity witch is equvivalent the new entity.

I tried to use a query without cache

$query = $this->getDoctrine()->getRepository('SSWTestBundle:SimpleEntity')->createNamedQuery(...);
...
$query->useResultCache(FALSE);

This don't solved my problem.

I tried to use a query without cache and with refresh

$query = $this->getDoctrine()->getRepository('SSWTestBundle:SimpleEntity')->createNamedQuery(...);
...
$query->useResultCache(FALSE);
$query->setHint(\Doctrine\ORM\Query::HINT_REFRESH, true);

This time I got a old entity from database, but the new entity was rollback to the old entity.

In attach is my entity and TestCase.



 Comments   
Comment by SergSW [ 07/Nov/12 ]

While I solved this problem so

I registered mine own hint.
I added folow code.

DoctrineORMQuery
/**
     * SSW
     * @var string
     */
    const HINT_CREATE_NEW_INSTANCE = 'doctrine.create.new.instance';

And

DoctrineORMUnitOfWork:line 1978
/* old code
if (isset($this->identityMap[$class->rootEntityName][$idHash])) {
            $entity = $this->identityMap[$class->rootEntityName][$idHash];
            $oid = spl_object_hash($entity);
...
*/
        if (isset($this->identityMap[$class->rootEntityName][$idHash])
                && !isset($hints[Query::HINT_CREATE_NEW_INSTANCE])) {
            $entity = $this->identityMap[$class->rootEntityName][$idHash];
            $oid = spl_object_hash($entity);
            if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
                $entity->__isInitialized__ = true;
...

I know it's wrong, because the new instanse overrided the old instance.

Comment by SergSW [ 07/Nov/12 ]

It's my fall.
I forgot deattach entity.

Generated at Fri Oct 24 13:05:07 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.