DDC-422: Unable to add entity in @ManyToMany association when owning entity is extended from another entity #4921

Closed
doctrinebot opened this Issue Mar 14, 2010 · 8 comments

1 participant

@doctrinebot

Jira issue originally created by user else:

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.

@doctrinebot

Comment created by else:

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 ShopData_EntityGuest to /*** @MappedSuperclass **/ i can find $class->associationMappings on same line with some elements.

Hope it help to fix it :-)

Andy

@doctrinebot

Comment created by else:

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
@doctrinebot

Comment created by else:

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

Andy

@doctrinebot

Comment created by else:

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

@doctrinebot

Comment created by romanb:

This is fixed now in trunk.

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by else:

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

@doctrinebot doctrinebot added this to the 2.0-BETA1 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment