The solution, that I use is that I create a Switcher on the root entity Repository
class, like so:
class PageRepository extends EntityRepository
{
protected $_switchEntityNameSave = null;
/**
* @param type $fqcn
*/
protected function _swapEntityDiscriminator($fqcn = null){
if(isset($fqcn)){
$this->_switchEntityNameSave = $this->_entityName;
$this->_entityName = $fqcn;
} else {
$this->_entityName = $this->_switchEntityNameSave;
unset($this->_switchEntityNameSave);
}
}
// ... AND TO USE...
public function findSomeStuff()
{
$this->_swapEntityDiscriminator(News::getFqcn());
// The query, the result in a variable, $q for example
$this->_swapEntityDiscriminator();
return $q->getQuery();
}
}
Then, in the parent classe, I do the Getter getFqcn()
, like so:
abstract class BaseEntity {
/**
* Get fqcn
*
* Fully Qualified Class Name
*
* @return string
*/
public static function getFqcn()
{
return get_called_class();
}
// ...
}
That use the Late static binding feature and give me the full class name on the concrete object (either News
or Page
).
I do put abstract, to be sure to not instantiate it.
What I also add in the concrete classes, a few constants:
class News extends Page {
const HUMAN_READABLE = "News";
const DISCRIMINATOR = "news"; // SAME as your @DiscriminatorMap() Annotation.
}
That way, my Repository
I can create specialized finders only on one of the types.
SELECT * FROM Page...
? – Cobby