doctrine / orm

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

DDC-1942: problem with serialize/merging entities with aggregation #2613

Closed doctrinebot closed 8 years ago

doctrinebot commented 12 years ago

Jira issue originally created by user gabrielnn77:

i have these two entities:

namespace nuevo_paquete;

class Clase_01{
    /****
    * @Id
    * @Column(name="id",type="integer",nullable=false)
    * @GeneratedValue
    */
    protected $id;

        /****
    * @ManyToOne(targetEntity="\paquete*02\Clase_03", inversedBy="clase_01", fetch="EXTRA*LAZY", cascade={"detach","merge"})
    * @JoinColumn(name="clase_03", referencedColumnName="id")
    */
    protected $clase_03;

        /****
    * @ManyToOne(targetEntity="\paquete*02\Clase_03", inversedBy="clase_01_1", fetch="EXTRA*LAZY", cascade={"detach","merge"})
    * @JoinColumn(name="clase*03*1", referencedColumnName="id")
    */
    protected $clase*03*1;
}
namespace paquete_02;
class Clase_03{
   /****
    * @Id
    * @Column(name="id",type="integer",nullable=false)
    * @GeneratedValue
    */
    protected $id;

        /****
    * @OneToMany(targetEntity="\nuevo*paquete\Clase_01", mappedBy="clase_03", fetch="EXTRA*LAZY", cascade={"detach"})
    */
    protected $clase_01;

    /****
    * @OneToMany(targetEntity="\nuevo*paquete\Clase_01", mappedBy="clase_03_1", fetch="EXTRA*LAZY", cascade={"detach"})
    */
    protected $clase*01*1;
}

Clase_01 have two aggregation association with Clase_03

Clase*01 ------> Clase*03
         |-----> Clase*03*1

when serialize an object of class Clase_01 and then unserialize, i lost the reference to the object of class Clase_03 but i can see Clase_03_1

ej:

$aux*orm = $em->find('nuevo_paquete\Clase*01', 1);
$em->detach($aux_orm);
$aux*s = serialize($aux*orm);
$aux*orm = unserialize($aux*s);

$aux*orm = $em->merge($aux*orm);
echo '['.$aux_orm->getId().']'; // [1]
echo '['.$aux*orm->getClase*03()->getId().']'; // [] expected : [1]
echo '['.$aux*orm->getClase_03*1()->getId().']'; // [1]

when i call get_clase_03() before detach the object, i can see the object Clase_03 ej:

$aux*orm = $em->find('nuevo_paquete\Clase*01', 1);
$aux*orm->getClase*03()->getId();

$em->detach($aux_orm);
$aux*s = serialize($aux*orm);
$aux*orm = unserialize($aux*s);

$aux*orm = $em->merge($aux*orm);
echo '['.$aux_orm->getId().']'; // [1]
echo '['.$aux*orm->getClase*03()->getId().']'; // [1]
echo '['.$aux*orm->getClase_03*1()->getId().']'; // [1]
doctrinebot commented 12 years ago

Comment created by gabrielnn77:

if i call $em->find() i still not able to view the object

//----------------------------------------
$aux*orm = $em->find('nuevo_paquete\Clase*01', 1);
$em->detach($aux_orm);
$aux*s = serialize($aux*orm);
$aux*orm = unserialize($aux*s);

$aux*orm = $em->merge($aux*orm);
echo '['.$aux_orm->getId().']'; // [1]
echo '['.$aux*orm->getClase*03()->getId().']'; // [] expected : [1]
echo '['.$aux*orm->getClase_03*1()->getId().']'; // [1]

$aux*orm = $em->find('nuevo_paquete\Clase*01', 1);
echo '['.$aux_orm->getId().']'; // [1]
echo '['.$aux*orm->getClase*03()->getId().']'; // still empty [] expected : [1]

//---------------------------------------- and if i have another register referencing the same object of class_03, i can't view ej:

$aux*orm = $em->find('nuevo_paquete\Clase*01', 2);
echo '['.$aux_orm->getId().']'; // [2]
echo '['.$aux*orm->getClase*03()->getId().']'; // still empty [] expected : [1]
doctrinebot commented 12 years ago

Comment created by amarquez:

i have the same problem

doctrinebot commented 12 years ago

Comment created by @beberlei:

formatted code

doctrinebot commented 12 years ago

Comment created by @beberlei:

Can you paste a var_dump() of class1 after you call detach and before serialize and then another one after you called unserialize? Also a var_dump of the serialized string $aux_s

doctrinebot commented 12 years ago

Comment created by gabrielnn77:

var_dump in attached file

doctrinebot commented 12 years ago

Comment created by gabrielnn77:

i found that if the class has more than two levels have the same problem by ex. i have to do

// class1 -> register id 1
// class2 -> register id 1
// class3 -> null, don't exist
$class2 = $class1->getClass2();
$class3 = $class2->getClass3();
$class3->getId();

otherwise doctrine insert new registers in database for class3 after unserialize

doctrinebot commented 12 years ago

Comment created by gsesia:

I have same problems

doctrinebot commented 12 years ago

Comment created by gabrielnn77:

if i declare in the entity annotation fetch="EAGER" works fine

doctrinebot commented 11 years ago

Comment created by valentin:

I have the same problem.

From what I saw, if I use fetch: EAGER, all my distinct entities are merged indeed, but if a specific entity (instance) is in two different collections, only one collection will have this instance.

So, any news about this bug ?

doctrinebot commented 11 years ago

Comment created by valentin:

I'm still trying to get my stuff works, and I'm now pretty sure that as soon as an object have to be merge more than once in a row (because of cascade associations), it end up being only in one collection, and lost for other entities.

I hope this could help.

doctrinebot commented 11 years ago

Comment created by @ocramius:

I think this one is also related with DDC-1734 (noticed a lot of un-initialized objects).

[~gabrielnn77] can you please try initializing ALL of these related objects prior to detaching/serializing and repeat your checks?

doctrinebot commented 11 years ago

Comment created by gabrielnn77:

How do I have to initialize the objects?

doctrinebot commented 11 years ago

Comment created by @ocramius:

[~gabrielnn77] by calling any method that is not getId on them. This will initialize them

doctrinebot commented 11 years ago

Comment created by @ocramius:

[~gabrielnn77] news on this one? Managed to verify it?

doctrinebot commented 11 years ago

Comment created by @ocramius:

This may be a duplicate of DDC-2306

doctrinebot commented 11 years ago

Comment created by @beberlei:

A related Github Pull-Request [GH-585] was closed https://github.com/doctrine/doctrine2/pull/585

doctrinebot commented 11 years ago

Comment created by @ocramius:

This issue is valid only before the fix introduced at DDC-2306. Duplicate confirmed

doctrinebot commented 10 years ago

Comment created by @doctrinebot:

A related Github Pull-Request [GH-585] was closed: https://github.com/doctrine/dbal/pull/585

doctrinebot commented 10 years ago

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1172] was assigned: https://github.com/doctrine/doctrine2/pull/1172

doctrinebot commented 9 years ago

Comment created by @doctrinebot:

A related Github Pull-Request [GH-1172] was closed: https://github.com/doctrine/doctrine2/pull/1172

doctrinebot commented 11 years ago

Issue was closed with resolution "Duplicate"