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

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

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

      Yii2:如何使用联合查询和排序创建 ActiveDataProvid

      时间:2023-05-21
    3. <small id='dI7Uw'></small><noframes id='dI7Uw'>

        <tbody id='dI7Uw'></tbody>
          <tfoot id='dI7Uw'></tfoot>

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

          1. <legend id='dI7Uw'><style id='dI7Uw'><dir id='dI7Uw'><q id='dI7Uw'></q></dir></style></legend>
              <bdo id='dI7Uw'></bdo><ul id='dI7Uw'></ul>
                本文介绍了Yii2:如何使用联合查询和排序创建 ActiveDataProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                使用 Yii 框架 2.0,我有两个数据库表,如下所示.

                With Yii framework 2.0 I have two database tables as following.

                A table:
                   a_id = 1, name = yes, number = 123
                   a_id = 2, name = no, number = 456 
                   a_id = 3, name = ok,  number = 683
                
                B table:
                  id = 1, a_id = 1, firstname = s
                  id = 2, a_id = 1, firstname = y
                  id = 3, a_id = 2, firstname = e
                  id = 4, a_id = 2, firstname = x
                  id = 5, a_id = 2, firstname = t
                  id = 6, a_id = 3, firstname = r
                

                我想使用 ActiveDataProvider for GridView 查询这些记录,结果如下.

                I would like to query these records using ActiveDataProvider for GridView and have the result as following.

                a_id = 1, name = yes, number = 123
                a_id = 1, name = s, number = null
                a_id = 1, name = y, number = null
                a_id = 2, name = no, number = 456
                a_id = 2, name = e, number = null
                a_id = 2, name = x, number = null
                a_id = 2, name = t, number = null
                a_id = 3, name = ok,  number = 683
                a_id = 3, name = r, number = null
                

                下面是我的纯 SQL 查询.

                Below is my working pure SQL query.

                SELECT `a_id`, `name`, `number` FROM `user` WHERE number != ''
                UNION ALL
                SELECT `a_id`, `firstname` as name , null as `number` FROM `customer` 
                WHERE `firstname` != ''
                ORDER BY `a_id` ASC, name ASC 
                

                我想用 ActiveDataProvider 实现上述查询.我该怎么做?

                I would like to implement this above query with ActiveDataProvider. How can I do that?

                推荐答案

                这样做:

                $query1 = (new yiidbQuery())
                    ->select("a_id, name, number")
                    ->from('user')
                    ->where(['!=', 'number', '']);
                
                $query2 = (new yiidbQuery())
                    ->select("a_id, firstname as name , null as number")
                    ->from('customer')
                    ->where(['!=', 'firstname', '']);
                
                $unionQuery = (new yiidbQuery())
                    ->from(['dummy_name' => $query1->union($query2)])
                    ->orderBy(['a_id' => SORT_ASC, 'name' => SORT_ASC]);
                
                $provider = new ActiveDataProvider([
                    'query' => $unionQuery,
                    'pagination' => [
                        'pageSize' => 20,
                    ],
                ]);
                
                $rows = $provider->getModels();
                

                它应该创建一个如下所示的查询:

                It should create a query that looks like this:

                SELECT * FROM 
                (
                    (SELECT `a_id`, `name`, `number` FROM `user` WHERE `number` != ''   )
                    UNION 
                    (SELECT `a_id`, `firstname` AS `name`, `null` AS `number` FROM `customer` WHERE `firstname` != '')
                ) `dummy_name`
                ORDER BY `a_id`, `name`
                

                它的灵感来自这个例子在 Yii 指南中.

                It is inspired by this example in the Yii guide.

                这篇关于Yii2:如何使用联合查询和排序创建 ActiveDataProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:yii2 中 gridview 中的图像 下一篇:Yii2:Kartik Gridview 页脚中一列的总和

                相关文章

                最新文章

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

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

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