Doctrine MongoDB ODM
  1. Doctrine MongoDB ODM
  2. MODM-33

Class-level annotations are ignored if set on MappedSuperclass

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0.0BETA1
    • Component/s: Mapping Drivers
    • Labels:
      None

      Description

      Class-level annotations: @HasLifecycleCallbacks, @InheritanceType, @DiscriminatorField, @DiscriminatorMap have no effect when defined on @MappedSuperclass

      You must explicitly set @HasLifecycleCallbacks for every document class to make callback work. As for inheritance annotations, you have to copy-paste all annotations to all document classes to make it work.

      Simple example:

      /**
       * @MappedSuperclass
       * @HasLifecycleCallbacks
       */
      abstract class a
      {
          /** @String */
          protected $a = '';
      
          /** @PrePersist */
          function prePersist() {$this->a = 'a';}
      }
      
      /** @Document(db="tests",collection="tests") */
      class b extends a
      {}
      
      $b = new b();
      $dm->persist($b);
      $dm->flush($b);
      

      With this example prePersist won't be called. Not sure if it is bug or expected behavior, but it adds some inconvenience, especially with inheritance mapping.

        Activity

        Hide
        Jonathan H. Wage added a comment -
        Show
        Jonathan H. Wage added a comment - This appears to work for me. I tested it here http://github.com/doctrine/mongodb-odm/commit/af9aa0f674f8b542c89f8c975ac403fd46f6e29e
        Hide
        Vladimir Razuvaev added a comment -

        Hi, looks like it affected ALPHA1.
        I just updated to latest version from git and this issue disappeared.

        Also, want to thank you and other committers for ODM project. It is really cool!

        Show
        Vladimir Razuvaev added a comment - Hi, looks like it affected ALPHA1. I just updated to latest version from git and this issue disappeared. Also, want to thank you and other committers for ODM project. It is really cool!
        Hide
        Vladimir Razuvaev added a comment -

        Looks like issue still exists for inheritance. At least following test case doesn't work for me on the latest version from git:

        /**
         * @MappedSuperClass
         * @InheritanceType("SINGLE_COLLECTION")
         * @DiscriminatorField(fieldName="type")
         * @DiscriminatorMap({
         *     "b"="b",
         *     "c"="c"
         * })
         */
        abstract class a
        {
            /** @String */
            protected $a = '';
        }
        
        /** @Document(db="tests",collection="tests") */
        class b extends a
        {}
        
        /** @Document(db="tests",collection="tests") */
        class c extends a
        {}
        
        $b = new b();
        $dm->persist($b);
        $dm->flush($b);
        

        New document will be created without "type" field. It works properly only if I copy-paste all annotations to every document class (b, c, etc).

        Show
        Vladimir Razuvaev added a comment - Looks like issue still exists for inheritance. At least following test case doesn't work for me on the latest version from git: /** * @MappedSuperClass * @InheritanceType( "SINGLE_COLLECTION" ) * @DiscriminatorField(fieldName= "type" ) * @DiscriminatorMap({ * "b" = "b" , * "c" = "c" * }) */ abstract class a { /** @ String */ protected $a = ''; } /** @Document(db= "tests" ,collection= "tests" ) */ class b extends a {} /** @Document(db= "tests" ,collection= "tests" ) */ class c extends a {} $b = new b(); $dm->persist($b); $dm->flush($b); New document will be created without "type" field. It works properly only if I copy-paste all annotations to every document class (b, c, etc).
        Hide
        Jonathan H. Wage added a comment -

        I don't think that is supposed to work. Try removing the @MappedSuperclass annotation.

        Show
        Jonathan H. Wage added a comment - I don't think that is supposed to work. Try removing the @MappedSuperclass annotation.
        Hide
        Vladimir Razuvaev added a comment -

        Removing @MappedSuperClass didn't help, but adding @Document annotation on abstract class did (it also allows to remove @Document annotation on concrete classes).

        The main point was to avoid annotations duplication, so I think this solution works pretty well. But maybe worth mentioning in the documentation, because it is pretty common use case to have one abstract class and several subclasses within same collection. And it took some time for me to find out how that could be done.

        Show
        Vladimir Razuvaev added a comment - Removing @MappedSuperClass didn't help, but adding @Document annotation on abstract class did (it also allows to remove @Document annotation on concrete classes). The main point was to avoid annotations duplication, so I think this solution works pretty well. But maybe worth mentioning in the documentation, because it is pretty common use case to have one abstract class and several subclasses within same collection. And it took some time for me to find out how that could be done.
        Show
        Jonathan H. Wage added a comment - Pretty sure it is fixed now. http://github.com/doctrine/mongodb-odm/commit/7ca29f25f7018b781183cf72082802ab4dcc567a

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Vladimir Razuvaev
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: