0

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 :)

Chris
  • 1
  • 2

1 Answers1

0

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.

Stefano Torresi
  • 686
  • 4
  • 10
  • Thanks. Though I think in the example above the AdminModule and PageModule would still need to be in the same namespace and therefore still leaving the coupling? Apologies if I have misunderstood. – Chris May 15 '13 at 11:10
  • yes, you'll probably have the ModuleSuperClass and the BaseModule in the namespace of a package that doesn't provide the PageModule, while having both AdminModule and PageModule in the package that does. – Stefano Torresi May 15 '13 at 11:15
  • I'm afraid that won't work with the namespace structure I indicated in the question. :( Thanks though. – Chris May 15 '13 at 11:50
  • well you can have an entirely different AdminModule in another bundle which is not aware of the PageModule, but still shares most of the functionality by extending the BaseModule. – Stefano Torresi Jun 05 '13 at 15:14