当运行下面的DQL时,从原则2中仅选择区分列时,我需要一些帮助

SELECT p.type FROM AppBundle\Entity\Product p
type是实体AppBundle\Entity\Product中的鉴别符列
@ORM\DiscriminatorColumn(name="type", type="smallint")`

@ORM\DiscriminatorMap({
    "0" = "AppBundle\Entity\Product",
    "1" = "AppBundle\Entity\Product\SingleIssue",
    "2" = "AppBundle\Entity\Product\CountBasedIssue",
    "3" = "AppBundle\Entity\Product\TimeBasedIssue"
})

我知道type在实体中不是不动产,但是我仍然可以这样做吗?

提前致谢!

更新

在阅读了两天的代码后,我决定重写SqlWalker并通过下面的代码片段创建新的Hydrator

覆盖SqlWalker
<?php

namespace ...;

use Doctrine\ORM\Query\SqlWalker;

class CustomSqlWalker extends SqlWalker
{
    const FORCE_GET_DISCRIMINATOR_COLUMN = 'forceGetDiscriminatorColumn';
    const DISCRIMINATOR_CLASS_MAP = 'discriminatorClassMap';

    /**
     * {@inheritdoc}
     */
    public function walkSelectClause($selectClause)
    {
        $sql = parent::walkSelectClause($selectClause);
        $forceGetDiscriminatorColumn = $this->getQuery()->getHint(self::FORCE_GET_DISCRIMINATOR_COLUMN);
        if (empty($forceGetDiscriminatorColumn)) {
            return $sql;
        }

        foreach ($this->getQueryComponents() as $key => $queryComponent) {
            if (!in_array($key, $forceGetDiscriminatorColumn)) {
                continue;
            }

            $metadata = $queryComponent['metadata'];
            $discriminatorColumn = $metadata->discriminatorColumn['name'];
            $tableName = $metadata->table['name'];
            $tableAlias = $this->getSQLTableAlias($tableName, $key);
            $discriminatorColumnAlias = $this->getSQLColumnAlias($discriminatorColumn);
            $sql .= ", $tableAlias.$discriminatorColumn AS $discriminatorColumnAlias";
        }

        return $sql;
    }
}

定制水合器
<?php

namespace ...;

use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use PDO;

class CustomHydrator extends ArrayHydrator
{
    /**
     * {@inheritdoc}
     */
    protected function hydrateAllData()
    {
        $result = array();

        $rootClassName = null;
        if (isset($this->_hints['forceGetDiscriminatorColumn']) &&
            isset($this->_hints['discriminatorClassMap'])) {
            $rootClassName = $this->_hints['discriminatorClassMap'];
        }

        while ($data = $this->_stmt->fetch(PDO::FETCH_ASSOC)) {
            foreach ($data as $key => $value) {
                if ($this->hydrateColumnInfo($key) != null ||
                    empty($rootClassName)) {
                    continue;
                }

                $metadata = $this->getClassMetadata($rootClassName);
                $discriminatorColumn = $metadata->discriminatorColumn;
                $fieldName = $discriminatorColumn['fieldName'];
                $type = $discriminatorColumn['type'];
                $this->_rsm->addScalarResult(
                    $key, $fieldName, $type
                );
            }
            $this->hydrateRowData($data, $result);
        }

        return $result;
    }
}

配置自定义水化器
orm:
    ...
    hydrators:
        CustomHydrator: YourNamespace\To\CustomHydrator

最后一步
$query = $queryBuilder->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'YourNamespace\To\CustomSqlWalker');
$query->setHint(\YourNamespace\To\CustomSqlWalker::FORCE_GET_DISCRIMINATOR_COLUMN, array($rootAlias)); // this alias will be used in CustomSqlWalker class
$query->setHint(\YourNamespace\To\CustomSqlWalker::DISCRIMINATOR_CLASS_MAP, $this->getClassName()); // this full-qualify class name will be used in CustomHydrator class

$products = $query->getResult('CustomHydrator');

TL; DR

我知道这是一个非常复杂的解决方案(可能只是针对我的情况),所以我希望有人可以给我另一种简单的方法来解决此问题,非常感谢!

最佳答案

没有直接访问“鉴别符”列的权限。



您可以使用INSTANCE OF DQL as described in the docs查询实体的类型。例如:

$query = $em->createQuery("SELECT product FROM AppBundle\Entity\AbstractProduct product WHERE product  INSTANCE OF AppBundle\Entity\Product");
$products = $query->getResult();

希望这可以帮助

关于php - 如何在准则2中选择区分列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33360211/

10-13 01:33