Doctrine 2 - Inheritance of table classes, selection by type

I have two objects - News and Page. The definition is as follows:

/ **
 * 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" objects - simple SELECT ... FROM News n.

But is there a way to select only "page" objects that map directly to the page class? Or do I need to create an additional object expanding the page for this?

+5
2

, , , Repository, :

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();
  }

}

, , Getter getFqcn(), :

abstract class BaseEntity {
  /**
   * Get fqcn
   * 
   * Fully Qualified Class Name
   *
   * @return string
   */
  public static function getFqcn()
  {
      return get_called_class();
  }
  // ...
}

Late static binding ( News, Page).

, .

, :

class News extends Page {
  const HUMAN_READABLE = "News";
  const DISCRIMINATOR = "news";   // SAME as your @DiscriminatorMap() Annotation.

}

, Repository .

0

All Articles