0
votes

I have 2 Symfony bundles. AdminBundle will always be installed. PageBundle may or may not be installed.

I want to define a base Entity called AdminModule (name, controller class, description, enabled), and also a PageModule which simply inherits from AdminModule ( the entities controller will implement a specific interface).

<?php

namespace AdminBundle\Entity;

/**
 * Admin Component
 *
 * @ORM\Entity
 * @ORM\Table(name="admin_module")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"page" = "\PageBundle\Entity\PageComponent"})
 */
class AdminModule
{
    // private vars, getters, setters
}
?>

<?php
namespace PageBundle\Entity;

use AdminBundle\Entity\AdminModule;

/**
 * Page Component
 *
 * @ORM\Entity
 * @ORM\Table(name="page_module")
 */
class PageModule extends AdminModule
{
    //
}
?>

The issue I have, I think, is that the AdminModule annotation @ORM\DiscriminatorMap({"page" = "\PageBundle\Entity\PageModule"}) requires definition on the AdminBundle - but the PageBundle may not be installed.

I believe must have the wrong type of inheritance structure (?) however I am not clear on what alternative approaches I can take? Thanks for any help or direction :)

1

1 Answers

0
votes

you can't do what you're trying to with table inheritance mappings, because you have to write annotations in the parent class, so the parent class itself ends up being coupled with his children.

what you could use is a mapped superclass (@MappedSuperclass) to extend the actual parent entities from.

all your common properties should then go into the mapped superclass, using its children as actual entities to define different inheritance mappings and associations (association mappings in mapped superclasses are very limited).

so in your specific case you could have such a structure:

/** 
 * I'm not an actual Entity!
 *
 * @MappedSuperClass */
Class ModuleSuperClass {}


/** 
 * I don't have children
 * 
 * @ORM\Entity */
Class BaseModule extends ModuleSuperClass {}

/** 
 * I have children
 * 
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"page" = "Page"}) 
 */
Class AdminModule extends ModuleSuperClass {}

/**
 * I'm just a child
 *
 * @ORM\Entity
 */
Class PageModule extends AdminModule {}

your mileage may of course vary, i.e. I would rather have a BaseModule class without children, and then a BaseModule in an entirely different namespace to extend both AdminModule and PageModule from.