Assume the following AbstractPage
model:
/*
* @ORM\\Entity
* @ORM\\Table(name=\"page\")
* @ORM\\InheritanceType(\"SINGLE_TABLE\")
*
Here is the solution I can think out. The idea is to extend some doctrine classes to add functionality required. Keep models as they are now.
Create new class extending SqlWalker (update namespaces of course)
<?php
namespace Sad;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\SqlWalker;
class CustomSqlWalker extends SqlWalker
{
const IGNORE_DISCRIMINATION = 'ignoreDiscrimination';
/**
* {@inheritdoc}
*/
public function walkWhereClause($whereClause)
{
// Get list of aliases in which discrimination should be ignored
$ignoreDescription = $this->getQuery()->getHint(self::IGNORE_DISCRIMINATION);
if ($ignoreDescription !== false) {
// For each aliases...
foreach ($this->getQueryComponents() as $k => $component) {
// ...check if alias is in ignore list
if (in_array($k, $ignoreDescription)) {
/** @var $metaObj ClassMetadata */
$metaObj = $component['metadata'];
// Update inheritance type to disable discrimination where
if ($metaObj->isInheritanceTypeSingleTable()) {
$metaObj->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_NONE);
}
}
}
}
return parent::walkWhereClause($whereClause);
}
}
Then making query you do the following:
echo $entityManager->createQuery("SELECT p FROM \Sad\Schema\AbstractPage as p")->getSQL();
// Output: ELECT p0_.id AS id_0, p0_.name AS name_1, p0_.type AS type_2 FROM page p0_ WHERE p0_.type IN ('page', 'link')
$query = $entityManager->createQuery("SELECT p FROM \Sad\Schema\AbstractPage as p");
// Make sure query uses custom walker
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Sad\CustomSqlWalker');
// Specify which aliases should ignore discrimination
$query->setHint(\Sad\CustomSqlWalker::IGNORE_DISCRIMINATION, array('p'));
echo $query->getSQL();
// Output: ELECT p0_.id AS id_0, p0_.name AS name_1, p0_.type AS type_2 FROM page p0_