[DDC-422] Unable to add entity in @ManyToMany association when owning entity is extended from another entity Created: 14/Mar/10  Updated: 18/Mar/10  Resolved: 18/Mar/10

Status: Closed
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.0-ALPHA4
Fix Version/s: 2.0-BETA1
Security Level: All

Type: Bug Priority: Blocker
Reporter: Ondrej Sibrina Assignee: Roman S. Borschel
Resolution: Fixed Votes: 0
Labels: None
Environment:

PHP 5.3.2


Attachments: File DDC406Test.php    

 Description   

Dear developers,

I'm not sure if i extend entities in right way but i found different behaviour in extended entity which won't process add in @ManyToMany association.
My class are defined like this:

/**
 * @Entity
 * @HasLifecycleCallbacks
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"guest" = "Shop_Data_Entity_Guest", "customer" = "Shop_Data_Entity_Customer"})
 */
class Shop_Data_Entity_Guest extends Shop_Data_Entity_Abstract {
    public function __construct() {
        /* call parent construct with all parameters */
        call_user_func_array(array("parent","__construct"),func_get_args());
    }
}

/**
 * @Entity
 * @HasLifecycleCallbacks
 */
class Shop_Data_Entity_Customer extends Shop_Data_Entity_Guest {
    /**
     * @ManyToMany(targetEntity="Shop_Data_Entity_Contact", cascade={"persist","remove"} )
     * @JoinTable(name="Shop_Data_Entity_Customer__homes",
     *      joinColumns={@JoinColumn(name="customer_id", referencedColumnName="id", onDelete="cascade" )},
     *      inverseJoinColumns={@JoinColumn(name="contact_id", referencedColumnName="id", onDelete="cascade" )}
     *      )
     */
    public $homes;

    public function __construct() {
        /* call parent construct with all parameters */
        call_user_func_array(array("parent","__construct"),func_get_args());

        $this->homes = new Shop_Data_Collection_Standard(); // this class only extends ArrayCollection
    }
}

When i fire this code:

        $q = $em->createQuery("select c from Shop_Data_Entity_Customer c");
        $q->setMaxResults(1);
        $customers = $q->getResult();
        
        $contact = new Shop_Data_Entity_Contact();
        $customers[0]->home->add($contact);
        $em->flush();

There's no change in database.
When i change definition od Guest class like this:

/** @MappedSuperclass */
class Shop_Data_Entity_Guest extends Shop_Data_Entity_Abstract {
    public function __construct() {
        /* call parent construct with all parameters */
        call_user_func_array(array("parent","__construct"),func_get_args());
    }
}

It start work and every firing of my code add one contact and association to my database. It causes problem only when owning entity is quered from database at first.



 Comments   
Comment by Ondrej Sibrina [ 16/Mar/10 ]

I tried to debug by myself and i found that on line 411 in UnitOfWork.php is on flush in first example $class->associationMappings == null. When i change Shop_Data_Entity_Guest to /** @MappedSuperclass */ i can find $class->associationMappings on same line with some elements.

Hope it help to fix it

Andy

Comment by Ondrej Sibrina [ 16/Mar/10 ]

I tried to made patch by myself and it looks it start working. I'm not sure if this solution is proper. Andy

from line 1731 of UnitOfWork.php

 if (isset($this->_identityMap[$class->name][$idHash])) {  // $$class->rootEntityName to $class->name
            $entity = $this->_identityMap[$class->name][$idHash]; // $class->rootEntityName to $class->name
            $oid = spl_object_hash($entity);
            if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
                $entity->__isInitialized__ = true;
                $overrideLocalValues = true;
            } else {
                $overrideLocalValues = isset($hints[Query::HINT_REFRESH]);
            }
        } else {
            //$entity = clone $class->prototype;
            $entity = new $className;
            $oid = spl_object_hash($entity);
            $this->_entityIdentifiers[$oid] = $id;
            $this->_entityStates[$oid] = self::STATE_MANAGED;
            $this->_originalEntityData[$oid] = $data;
            $this->_identityMap[$class->name][$idHash] = $entity; // $class->rootEntityName to $class->name
Comment by Ondrej Sibrina [ 16/Mar/10 ]

To ensure i checkouted trunk version but it doesn't solve this problem.

Andy

Comment by Ondrej Sibrina [ 16/Mar/10 ]

After using my patch i can't do $em->remove properly any other patch?

Comment by Roman S. Borschel [ 18/Mar/10 ]

This is fixed now in trunk.

Comment by Ondrej Sibrina [ 18/Mar/10 ]

Thank you very much Hope this report was better than my first one.

Generated at Thu Aug 21 04:28:43 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.