<tfoot id='bWT39'></tfoot><legend id='bWT39'><style id='bWT39'><dir id='bWT39'><q id='bWT39'></q></dir></style></legend>

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

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

          <bdo id='bWT39'></bdo><ul id='bWT39'></ul>

        使用 Yii2 中的关系在 GridView 列中搜索

        时间:2023-10-15

        <small id='3a4bi'></small><noframes id='3a4bi'>

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

              <bdo id='3a4bi'></bdo><ul id='3a4bi'></ul>
                <tbody id='3a4bi'></tbody>

                • 本文介绍了使用 Yii2 中的关系在 GridView 列中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有外键 city_id 的模型,它引用了表 Cities (id).在 GridView 中,我显示城市标题而不是 id,并且我知道如何添加搜索带有外键的列,即在视图中:

                  I have a model with foreign key city_id that references table Cities (id). In the GridView I show city title instead of id, and I know how to add searching for column with foreign key, i.e, in view:

                  GridView::widget([
                      'dataProvider' => $dataProvider,
                      'filterModel' => $searchModel,
                      'columns' => [
                         //some columns
                          [ 
                              'attribute' => 'city_id',
                              'value'=>'city.title
                          ],
                  ]);
                  

                  并在搜索模型(search() 函数)中添加:

                  And in search model (search() function) I add:

                  $query->joinWith('city');
                  $query->andFilterWhere(['like', 'cities.title', $this->city_id]);
                  

                  而且它工作正常.但我也想在我的网格中显示 region,我的表 Cities 有一个外键 region_id 引用 Regions(id).我在网格中添加了区域标题:

                  And it works fine. But I also want to show region in my grid, and my table Cities has a foreign key region_id which references Regions(id). I added the region title to the grid:

                   GridView::widget([
                      'dataProvider' => $dataProvider,
                      'filterModel' => $searchModel,
                      'columns' => [
                         //some columns
                          [ 
                              'attribute' => 'city_id',
                              'value'=>'city.title
                          ],
                          'city.region.title_en',
                  ]);
                  

                  现在我不知道如何添加搜索以及如何更改search() 函数,因为我的主模型中没有region_id,我通过城市.如有任何建议,我将不胜感激.

                  Now I dont know how to add searching and how to change search() function, because I dont have region_id in my main model, I retrieve it through city. I would appreciate any advise.

                  推荐答案

                  不要忘记将 region_id 添加到您的搜索模型中.

                  do not forget to add region_id to your search model.

                  这篇关于使用 Yii2 中的关系在 GridView 列中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <legend id='WOQpq'><style id='WOQpq'><dir id='WOQpq'><q id='WOQpq'></q></dir></style></legend>
                    • <bdo id='WOQpq'></bdo><ul id='WOQpq'></ul>
                        <tfoot id='WOQpq'></tfoot>

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

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

                            <tbody id='WOQpq'></tbody>