<bdo id='Aex9p'></bdo><ul id='Aex9p'></ul>
<legend id='Aex9p'><style id='Aex9p'><dir id='Aex9p'><q id='Aex9p'></q></dir></style></legend>

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

  1. <tfoot id='Aex9p'></tfoot>

      <i id='Aex9p'><tr id='Aex9p'><dt id='Aex9p'><q id='Aex9p'><span id='Aex9p'><b id='Aex9p'><form id='Aex9p'><ins id='Aex9p'></ins><ul id='Aex9p'></ul><sub id='Aex9p'></sub></form><legend id='Aex9p'></legend><bdo id='Aex9p'><pre id='Aex9p'><center id='Aex9p'></center></pre></bdo></b><th id='Aex9p'></th></span></q></dt></tr></i><div id='Aex9p'><tfoot id='Aex9p'></tfoot><dl id='Aex9p'><fieldset id='Aex9p'></fieldset></dl></div>
    1. 加入子查询与学说 2 DBAL

      时间:2023-08-19

      • <bdo id='l1Dr0'></bdo><ul id='l1Dr0'></ul>

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

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

                <legend id='l1Dr0'><style id='l1Dr0'><dir id='l1Dr0'><q id='l1Dr0'></q></dir></style></legend>
                  <tbody id='l1Dr0'></tbody>

              • 本文介绍了加入子查询与学说 2 DBAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在重构 Zend Framework 2 应用程序以使用 Dotct 2.5 DBAL 而不是 Zend_DB (ZF1).我有以下 Zend_Db 查询:

                I'm refactoring a Zend Framework 2 application to use doctrine 2.5 DBAL instead of Zend_DB (ZF1). I have the following Zend_Db query:

                $subSelect = $db->select()
                    ->from('user_survey_status_entries', array('userSurveyID', 'timestamp' => 'MIN(timestamp)'))
                    ->where('status = ?', UserSurveyStatus::ACCESSED)
                    ->group('userSurveyID');
                
                
                $select = $db->select()
                    // $selectColNames contains columns both from the main query and 
                    // the subquery (e.g. firstAccess.timestamp AS dateFirstAccess).
                    ->from(array('us' => 'user_surveys'), $selectColNames)
                    ->joinLeft(array('firstAccess' => $subSelect), 'us.userSurveyID = firstAccess.userSurveyID', array())
                    ->where('us.surveyID = ?', $surveyID);
                

                这会导致以下 MySQL 查询:

                This results in the following MySQL query:

                SELECT `us`.`userSurveyID`, 
                    // More columns from main query `us`
                    `firstAccess`.`timestamp` AS `dateFirstAccess`
                FROM `user_surveys` AS `us`
                LEFT JOIN (
                    SELECT `user_survey_status_entries`.`userSurveyID`, 
                            MIN(timestamp) AS `timestamp` 
                    FROM `user_survey_status_entries` 
                    WHERE (status = 20) 
                    GROUP BY `userSurveyID`
                ) AS `firstAccess` ON us.userSurveyID = firstAccess.userSurveyID 
                WHERE (us.surveyID = '10')
                

                我不知道如何使用doctrine 2.5 查询构建器加入子查询.在主查询中,我需要从子查询中选择列.

                I can't figure out how to join the subquery using the doctrine 2.5 query builder. In the main query, I need to select columns from the subquery.

                我在此处阅读了该信条不支持加入子查询.如果这仍然是真的,我可以使用doctrine DBAL 的SQL 查询构建器以另一种方式编写此查询吗?Native SQL 对我来说可能不是一个好的解决方案,因为这个查询将在代码后面动态扩展.

                I have read here that doctrine does not support joining subqueries. If that's still true, can I write this query in another way using the SQL query builder of doctrine DBAL? Native SQL may not be a good solution for me, as this query will be dynamically extended later in the code.

                推荐答案

                我通过调整这个DQL 示例 到 DBAL.诀窍是获取子查询的原始 SQL,将其包装在方括号中,然后加入它.子查询中使用的参数必须在主查询中设置:

                I've found a solution by adapting this DQL example to DBAL. The trick is to get the raw SQL of the subquery, wrap it in brackets, and join it. Parameters used in the subquery must be set in the main query:

                $subSelect = $connection->createQueryBuilder()
                    ->select(array('userSurveyID', 'MIN(timestamp) timestamp'))
                    ->from('user_survey_status_entries')
                    // Instead of setting the parameter in the main query below, it could be quoted here:
                    // ->where('status = ' . $connection->quote(UserSurveyStatus::ACCESSED))
                    ->where('status = :status')
                    ->groupBy('userSurveyID');
                
                $select = $connection->createQueryBuilder()
                    ->select($selectColNames)
                    ->from('user_surveys', 'us')
                    // Get raw subquery SQL and wrap in brackets.
                    ->leftJoin('us', sprintf('(%s)', $subSelect->getSQL()), 'firstAccess', 'us.userSurveyID = firstAccess.userSurveyID')
                    // Parameter used in subquery must be set in main query.
                    ->setParameter('status', UserSurveyStatus::ACCESSED)
                    ->where('us.surveyID = :surveyID')->setParameter('surveyID', $surveyID);
                

                这篇关于加入子查询与学说 2 DBAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:“我们"一个 DoctrineCommonCollectionsArrayCollection? 下一篇:ZF2 + Doctrine 2 - 具有类表继承的子级鉴别器

                相关文章

                • <bdo id='8t53S'></bdo><ul id='8t53S'></ul>
                  1. <i id='8t53S'><tr id='8t53S'><dt id='8t53S'><q id='8t53S'><span id='8t53S'><b id='8t53S'><form id='8t53S'><ins id='8t53S'></ins><ul id='8t53S'></ul><sub id='8t53S'></sub></form><legend id='8t53S'></legend><bdo id='8t53S'><pre id='8t53S'><center id='8t53S'></center></pre></bdo></b><th id='8t53S'></th></span></q></dt></tr></i><div id='8t53S'><tfoot id='8t53S'></tfoot><dl id='8t53S'><fieldset id='8t53S'></fieldset></dl></div>
                    <tfoot id='8t53S'></tfoot>

                    <small id='8t53S'></small><noframes id='8t53S'>

                    <legend id='8t53S'><style id='8t53S'><dir id='8t53S'><q id='8t53S'></q></dir></style></legend>