4
votes

I got following UML scheme : enter image description here

Basically, it's the beginning of taxonomy system with some of them nestable, and some not. I started to try making 2 layers of abstract classes (Taxonomy and OfferCategory) because none of them can be used as a final entity. I used MappedSuperClass, but I got following error :

[Doctrine\ORM\ORMException]                                                                                       
Column name `id` referenced for relation from LCH\CatalogBundle\Entity\HomeOfferCategory towards LCH\CatalogBund       le\Entity\OfferCategory does not exist.

My primary key field is id...

In a more general point of view, what's the best implementation for my scheme provided with Doctrine?

Thanks !

EDIT : I tried to transpose all OfferCategory members directly in my RootOfferCategory class. By changing th targetENtity on both sides, no more error. Meaning you can't self reference a mapped super class?

Taxonomy :

/**
* Class Taxonomy
* @package LCH\CatalogBundle\Entity
* @ORM\MappedSuperclass
*/
abstract class Taxonomy implements TaxonomyInterface
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string the category name
     * @ORM\Column(name="name", type="string", length=255)
     */
     protected $name;
}

**OfferCategory : **

/**
 * OfferCategory
 * @ORM\MappedSuperclass
 */
abstract class OfferCategory extends Taxonomy
{
   /**
    * @var OfferCategory the category parent
    * @ORM\ManyToOne(targetEntity="LCH\CatalogBundle\Entity\OfferCategory",inversedBy="children", cascade={"persist"})
    * @ORM\JoinColumn(name="parent_id", referenceColumnName="id")
    */
    protected $parent;
   /**
   * @var OfferCategory the children categories
   *       @ORM\OneToMany(targetEntity="LCH\CatalogBundle\Entity\OfferCategory",mappedBy="parent", cascade={"persist"})
   */
   protected $children;
}

RootOfferCategory

/**
 * RootOfferCategory
 * Represents one root top category
 * @ORM\Table()
 *      @ORM\Entity(repositoryClass="LCH\CatalogBundle\Entity\RootOfferCategoryRepository")
 */
class RootOfferCategory extends OfferCategory
{

}
1

1 Answers

0
votes

Sorry to arrive after the war.

From this part of the Doctrine documentation :

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only).

This means that One-To-Many associations are not possible on a mapped superclass at all.
Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity.At the moment.

For further support of inheritance, the single or joined table inheritance features have to be used.