Symfony2 MongoDB Doctrine Inheritance

603 Views Asked by At

I come with a little problem that I tried to understand in vain... I have 2 classes. One is an abstract Document called "SpecificIndividual" and the other one is a regular Document called "Individual".

This is what I want to do : SpecificIndividual has some properties and some getters/setters methods. Individual inherits SpecificIndividual and has its own properties and getters/setters methods.

I used MappedSuperClass inheritance with COLLECTION_PER_CLASS type.

My problem is that when I use "doctrine:mongodb:generate:documents" command, it generates all getters/setters methods for the abstract class (this is what I want), all getters/setters methods for the child class (this is also what I want) BUT it duplicates all of the abstract class getters/setters methods into the child class (this has no sense at all in inheritance).

I'm probably doing something wrong, since it worked when I was using MySQL and Entities, but since I moved to MongoDB, I can't find the proper way to do this.

Here are my classes : First, SpecificIndividual :

namespace SpecificBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * SpecificIndividual
 *
 * @MongoDB\Document
 * @MongoDB\InheritanceType("COLLECTION_PER_CLASS")
 */
abstract class Individual
{
    /**
     * @MongoDB\Id
     */
    protected $individual_id;

    /**
     * @MongoDB\Boolean
     */
    protected $chiped;

    /**
     * Get individualId
     *
     * @return id $individualId
     */
    public function getIndividualId()
    {
        return $this->individual_id;
    }

    /**
     * Set chiped
     *
     * @param boolean $chiped
     * @return self
     */
    public function setChiped($chiped)
    {
        $this->chiped = $chiped;
        return $this;
    }

    /**
     * Get chiped
     *
     * @return boolean $chiped
     */
    public function getChiped()
    {
        return $this->chiped;
    }
}

and the child class Individual :

namespace ChildBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

use SpecificBundle\Document\Individual as SpecificIndividual;

/**
 * Individual
 *
 * @MongoDB\Document(collection="individuals")
 */
class Individual extends SpecificIndividual
{
    /**
     * @MongoDB\String
     */
    protected $someString;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * Set someString
     *
     * @param String $someString
     * @return self
     */
    public function setSomeString(String $someString)
    {
        $this->someString = $someString;
        return $this;
    }

    /**
     * Get someString
     *
     * @return String $someString
     */
    public function getSomeString()
    {
        return $this->someString;
    }
}

I count on you all guys for some help ! Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

You should add "@MongoDB\MappedSuperclass" to annotation of your abstract class

Link do the documentation http://doctrine-orm.readthedocs.org/projects/doctrine-mongodb-odm/en/latest/reference/inheritance-mapping.html

Your code sample

namespace SpecificBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * SpecificIndividual
 * 
 * @MongoDB\MappedSuperclass
 * @MongoDB\Document
 * @MongoDB\InheritanceType("COLLECTION_PER_CLASS")
 */
abstract class Individual
{