<legend id='67PEw'><style id='67PEw'><dir id='67PEw'><q id='67PEw'></q></dir></style></legend>
    <i id='67PEw'><tr id='67PEw'><dt id='67PEw'><q id='67PEw'><span id='67PEw'><b id='67PEw'><form id='67PEw'><ins id='67PEw'></ins><ul id='67PEw'></ul><sub id='67PEw'></sub></form><legend id='67PEw'></legend><bdo id='67PEw'><pre id='67PEw'><center id='67PEw'></center></pre></bdo></b><th id='67PEw'></th></span></q></dt></tr></i><div id='67PEw'><tfoot id='67PEw'></tfoot><dl id='67PEw'><fieldset id='67PEw'></fieldset></dl></div>
    <tfoot id='67PEw'></tfoot>
    • <bdo id='67PEw'></bdo><ul id='67PEw'></ul>

  1. <small id='67PEw'></small><noframes id='67PEw'>

    1. 如何在学说 2 中选择鉴别器列

      时间:2023-08-19
      <tfoot id='cPCff'></tfoot>

        <tbody id='cPCff'></tbody>

          <i id='cPCff'><tr id='cPCff'><dt id='cPCff'><q id='cPCff'><span id='cPCff'><b id='cPCff'><form id='cPCff'><ins id='cPCff'></ins><ul id='cPCff'></ul><sub id='cPCff'></sub></form><legend id='cPCff'></legend><bdo id='cPCff'><pre id='cPCff'><center id='cPCff'></center></pre></bdo></b><th id='cPCff'></th></span></q></dt></tr></i><div id='cPCff'><tfoot id='cPCff'></tfoot><dl id='cPCff'><fieldset id='cPCff'></fieldset></dl></div>
            <bdo id='cPCff'></bdo><ul id='cPCff'></ul>
              <legend id='cPCff'><style id='cPCff'><dir id='cPCff'><q id='cPCff'></q></dir></style></legend>

              <small id='cPCff'></small><noframes id='cPCff'>

              1. 本文介绍了如何在学说 2 中选择鉴别器列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在运行下面的 DQL 时仅从原则 2 中选择鉴别器列时我需要一些帮助

                I need some help when select only discriminator column from doctrine 2 when run the DQL below

                SELECT p.type FROM AppBundleEntityProduct p
                

                type 是实体 AppBundleEntityProduct

                @ORMDiscriminatorColumn(name="type", type="smallint")`
                
                @ORMDiscriminatorMap({
                    "0" = "AppBundleEntityProduct",
                    "1" = "AppBundleEntityProductSingleIssue",
                    "2" = "AppBundleEntityProductCountBasedIssue",
                    "3" = "AppBundleEntityProductTimeBasedIssue"
                })
                

                我知道 type 不是实体中的真正属性,但无论如何我可以这样做吗?

                I know that type is not a real property in entity, but is there anyway for me to do that?

                提前致谢!

                在阅读了 2 天的 Doctrine 代码后,我决定覆盖 SqlWalker 并通过下面的代码片段创建新的 Hydrator

                After 2 days for reading Doctrine codes, I decided to override SqlWalker and create new Hydrator by the snippets below

                <?php
                
                namespace ...;
                
                use DoctrineORMQuerySqlWalker;
                
                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 DoctrineORMInternalHydrationArrayHydrator;
                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: YourNamespaceToCustomHydrator
                

                最后一步

                $query = $queryBuilder->getQuery();
                $query->setHint(DoctrineORMQuery::HINT_CUSTOM_OUTPUT_WALKER, 'YourNamespaceToCustomSqlWalker');
                $query->setHint(YourNamespaceToCustomSqlWalker::FORCE_GET_DISCRIMINATOR_COLUMN, array($rootAlias)); // this alias will be used in CustomSqlWalker class
                $query->setHint(YourNamespaceToCustomSqlWalker::DISCRIMINATOR_CLASS_MAP, $this->getClassName()); // this full-qualify class name will be used in CustomHydrator class
                
                $products = $query->getResult('CustomHydrator');
                

                TL;DR

                我知道这是一个非常复杂的解决方案(可能仅适用于我的场景),所以我希望有人能给我另一种简单的方法来解决这个问题,非常感谢!

                TL;DR

                I know this is a very complicated solution (may be just for my scenario), so I hope someone could give me another simple way to fix that, thanks so much!

                推荐答案

                没有直接访问鉴别器列.

                There is no direct access to the discriminator column.

                可能会发生特殊类型的实体应该被查询的情况.因为没有直接访问鉴别器列,Doctrine 提供了 INSTANCE OF 结构.

                It may happen that the entities of a special type should be queried. Because there is no direct access to the discriminator column, Doctrine provides the INSTANCE OF construct.

                您可以使用 INSTANCE OF DQL 如文档中所述.例如:

                You can query for the type of your entity using the INSTANCE OF DQL as described in the docs. As example:

                $query = $em->createQuery("SELECT product FROM AppBundleEntityAbstractProduct product WHERE product  INSTANCE OF AppBundleEntityProduct");
                $products = $query->getResult();
                

                希望能帮到你

                这篇关于如何在学说 2 中选择鉴别器列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Symfony2:找不到基表或视图:1146 下一篇:Symfony2 Doctrine PDO MySQL 连接与 LOAD DATA LOCAL INFILE

                相关文章

                      <bdo id='79JPN'></bdo><ul id='79JPN'></ul>
                    <tfoot id='79JPN'></tfoot>
                    <i id='79JPN'><tr id='79JPN'><dt id='79JPN'><q id='79JPN'><span id='79JPN'><b id='79JPN'><form id='79JPN'><ins id='79JPN'></ins><ul id='79JPN'></ul><sub id='79JPN'></sub></form><legend id='79JPN'></legend><bdo id='79JPN'><pre id='79JPN'><center id='79JPN'></center></pre></bdo></b><th id='79JPN'></th></span></q></dt></tr></i><div id='79JPN'><tfoot id='79JPN'></tfoot><dl id='79JPN'><fieldset id='79JPN'></fieldset></dl></div>

                  1. <small id='79JPN'></small><noframes id='79JPN'>

                    <legend id='79JPN'><style id='79JPN'><dir id='79JPN'><q id='79JPN'></q></dir></style></legend>