Leave out discriminator part of Doctrine' generated SQL

前端 未结 1 1826
北恋
北恋 2021-02-09 03:37

Assume the following AbstractPage model:

/*
 *     @ORM\\Entity
 *     @ORM\\Table(name=\"page\")
 *     @ORM\\InheritanceType(\"SINGLE_TABLE\")
 *          


        
1条回答
  •  执念已碎
    2021-02-09 03:52

    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)

    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_
    

    0 讨论(0)
提交回复
热议问题