PostPersist, PostUpdate, PostRemove are called twice in documents with inheritance #474

Closed
afgar opened this Issue Jan 8, 2013 · 5 comments

Comments

Projects
None yet
3 participants

afgar commented Jan 8, 2013

Hi,
I am working with Symfony master version, MongoDB and mongodb-odm-bundle master version.

I have somethig like this:

/**
 * @MongoDB\Document (collection="foo")
 * @MongoDB\InheritanceType("SINGLE_COLLECTION")
 * @MongoDB\DiscriminatorField(fieldName="type")
 * @MongoDB\DiscriminatorMap({"foo"="Foo"})
 */
abstract class AParentFoo
{

    /** @MongoDB\Id */
    protected $id;

    public function getId()
    {
    return $this->id;
    }

    /**
     * @MongoDB\PostPersist()
     */
    public function onPostPersist()
    {
    echo 'PostPersist';
    }

    /**
     * @MongoDB\PostUpdate()
     */
    public function onPostUpdate()
    {
    echo 'PostUpdate';
    }

    /**
     * @MongoDB\PostRemove()
     */
    public function onPostRemove()
    {
    echo 'PostRemove';
    }
}

/**
 * @MongoDB\Document
 */
class Foo extends AParentFoo {

    /** @MongoDB\String */
    protected $foovar;
}

When I save a Foo object ($dm->persist($foo); $dm->flush;), the application executes twice onPostPersist method. This is tthe same for postUpdate and postDelete.

Is this a bug? What can I do?

Thanks

Owner

jmikola commented Jan 9, 2013

This may be because your abstract class is annotated as a Document instead of a MappedSuperclass. I believe the class metadata logic registers callbacks only for Document classes, so this is a case of the method being registered twice for each Document-annotated class. Let me know if that helps.

afgar commented Jan 9, 2013

Yes..., but a mapped superclass cannot be a document and is not query able, and I need this feature. I want to execute an abstract method after querying by id on the foo collection that contains all the child documents.

How can I solve this?

Thanks

Contributor

Bilge commented Jan 29, 2013

See also #427.

Owner

jmikola commented Jan 9, 2014

Confirming that this is the same issue as #695 and will be fixed in #428. I'll note that lifecycle callbacks are processed and registered the same for classes annotated with @MappedSuperclass and @Document.

@jmikola jmikola added a commit that referenced this issue Jan 9, 2014

@jmikola jmikola Do not allow duplicate lifecycle callbacks to be registered
Using the method name as a key in lifecycleCallbacks will ensure that methods are never registered (and invoked) multiple times. We'll still use the method name as the value so existing code can iterate over values in a lifecycleCallbacks event array to get method names.

Fixes #427, #474, and #695. Related to doctrine/doctrine2#903.
59a67f3
Owner

jmikola commented Jan 9, 2014

#428 is merged, so this should be resolved.

jmikola closed this Jan 9, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment