Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
afgar opened this Issue · 5 comments

3 participants

@afgar

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

@jmikola
Owner

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

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

@Bilge

See also #427.

@jmikola
Owner

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 referenced this issue from a commit
@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
@jmikola
Owner

#428 is merged, so this should be resolved.

@jmikola jmikola closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.