I have two entities - News and Page. Definition looks like this:
/** * Description of Page * @Entity * @InheritanceType("JOINED") * @DiscriminatorColumn(name="type", type="string") * @DiscriminatorMap({ * "news" = "News" , * "page" = "Page"}) * * @table( * name="pages" * ) */ class Page extends BaseEntity {...} class News extends Page {...}
I know, how to select only "news" entities - simple SELECT ... FROM News n
.
But is there some way how to select only "page" entities, which are mapped directly to Page class? Or do I have to create some extra entity extending Page for this?
The solution is to use x INSTANCE OF Entity
in WHERE clause.
http://groups.google.com/group/doctrine-user/browse_thread/thread/b1dc52ed447204e2
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With