<tfoot id='sormY'></tfoot>

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

      1. <legend id='sormY'><style id='sormY'><dir id='sormY'><q id='sormY'></q></dir></style></legend>
        • <bdo id='sormY'></bdo><ul id='sormY'></ul>

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

        Yii - 如何通过管理页面上的外键/相关键的列进行搜索?

        Yii - how can I search by a column from foreign/related key on admin page?(Yii - 如何通过管理页面上的外键/相关键的列进行搜索?)

          • <legend id='r2Nwu'><style id='r2Nwu'><dir id='r2Nwu'><q id='r2Nwu'></q></dir></style></legend>

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

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

                1. <i id='r2Nwu'><tr id='r2Nwu'><dt id='r2Nwu'><q id='r2Nwu'><span id='r2Nwu'><b id='r2Nwu'><form id='r2Nwu'><ins id='r2Nwu'></ins><ul id='r2Nwu'></ul><sub id='r2Nwu'></sub></form><legend id='r2Nwu'></legend><bdo id='r2Nwu'><pre id='r2Nwu'><center id='r2Nwu'></center></pre></bdo></b><th id='r2Nwu'></th></span></q></dt></tr></i><div id='r2Nwu'><tfoot id='r2Nwu'></tfoot><dl id='r2Nwu'><fieldset id='r2Nwu'></fieldset></dl></div>
                    <tbody id='r2Nwu'></tbody>
                  <tfoot id='r2Nwu'></tfoot>
                  本文介绍了Yii - 如何通过管理页面上的外键/相关键的列进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的数据库中,我有两个表,制造商和塑料:

                  In my database, I have two tables, manufacturer and plastic:

                  CREATE TABLE `manufacturer` (                                                                   
                    `id` int(11) NOT NULL AUTO_INCREMENT,                                                                          
                    `name` varchar(64) DEFAULT NULL,                                                                               
                    PRIMARY KEY (`id`)                                                                                             
                  ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8
                  
                  CREATE TABLE `plastic` (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `name` varchar(64) NOT NULL DEFAULT '',
                    `manufacturer_id` int(11) NOT NULL DEFAULT '0',
                    PRIMARY KEY (`id`),
                    UNIQUE KEY `name` (`name`,`manufacturer_id`),
                    KEY `manufacturer_id` (`manufacturer_id`),
                    CONSTRAINT `plastic_ibfk_1` FOREIGN KEY (`manufacturer_id`) REFERENCES `manufacturer` (`id`)
                  ) ENGINE=InnoDB AUTO_INCREMENT=68 DEFAULT CHARSET=utf8
                  

                  如您所见,塑料与制造商之间存在着 Yii 所谓的 BELONGS_TO 关系——一个制造商可以生产多种不同的塑料.

                  As you can see, the plastic has what Yii would call a BELONGS_TO relationship with Manufacturer - one manufacturer could make several different plastics.

                  我试图让在默认塑料管理页面中按制造商名称搜索成为可能,但制造商"列没有显示搜索字段.我遵循了本指南我想我快到了,但我被困在一个小细节上.

                  I am trying to make it possible to search by manufacturer name from the default plastics admin page, but the search field does not show up for the Manufacturer column. I followed this guide and I think I am almost there, but I'm stuck on one tiny detail.

                  在我的塑料模型类中,根据上面的链接,我有:

                  In my Plastic model class, as per the link above, I have:

                  class Plastic extends CActiveRecord
                  {
                  public $manufacturer_search; //<-- added per the above link
                  
                  public function rules()
                  {
                      return array(
                          array('manufacturer.name', 'length', 'max'=>64),
                          array('name', 'length', 'max'=>64),
                          array('name, manufacturer.name, manufacturer_search', 'safe', 'on'=>'search'),
                      );
                  }
                  
                  public function relations()
                  {
                      return array(
                          'manufacturer' => array(self::BELONGS_TO, 'Manufacturer', 'manufacturer_id'),
                      );
                  }
                  
                  public function search()
                  {
                      $criteria=new CDbCriteria;
                      $criteria->with=array('manufacturer');
                      $criteria->compare('name',$this->name,true);
                      $criteria->compare('manufacturer.name',$this->manufacturer_search, true);
                  
                      return new CActiveDataProvider($this, array(
                          'criteria'=>$criteria,
                      ));
                  }
                  

                  }

                  管理页面使用 CGridView 小部件来显示所有内容(这是默认设置,除了 'columns' 属性我没有更改任何内容):

                  The admin page uses a CGridView widget to display everything (this is the default, I haven't changed anything except the 'columns' attribute):

                  <?php $this->widget('zii.widgets.grid.CGridView', array(
                      'id'=>'plastic-grid',
                      'dataProvider'=>$model->search(),
                      'filter'=>$model,
                      'columns'=>array(
                          'name',
                          'manufacturer.name',
                          array(
                              'class'=>'CButtonColumn',
                          ),  
                      ),  
                  )); ?>
                  

                  令人抓狂的是,搜索确实有效:如果我点击高级搜索,然后在制造商字段中输入一些内容,那就有效了.但我一辈子都无法让搜索框显示在网格视图中.

                  The maddening thing is that the search actually works: if I click on advanced search, and enter something into the manufacturer field, that works. But I can't for the life of me make the search box show up in the grid view.

                  这里是一些截图:当我将manufacturer_id传递给小部件时管理页面的截图,一个像上面的代码一样通过manufacturer.name时的截图,通过manufacturer_search时的截图(我没想到会起作用),最后是高级搜索正常工作的截图.

                  Here are some screenshots: a screenshot of the admin page when I pass manufacturer_id into the widget, a screenshot when I pass manufacturer.name like the code above, a screenshot of when I pass manufacturer_search (which I didn't expect to work anyway), and finally a screenshot of the advanced search working properly.

                  有什么想法吗?谢谢.

                  推荐答案

                  你必须专门为 $manufacturer_search 创建一个 filter 像:

                  You have to create a filter specifically for $manufacturer_search like:

                  <?php $this->widget('zii.widgets.grid.CGridView', array(
                      'id'=>'plastic-grid',
                      'dataProvider'=>$model->search(),
                      'filter'=>$model,
                      'columns'=>array(
                          'name',
                          array(
                              'name'=>'manufacturer.name',
                              'filter'=>CHtml::activeTextField($model,'manufacturer_search'),
                          ),
                          array(
                              'class'=>'CButtonColumn',
                          ),  
                      ),  
                  )); ?>
                  

                  这篇关于Yii - 如何通过管理页面上的外键/相关键的列进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                  1. <tfoot id='fPL3a'></tfoot>
                    <legend id='fPL3a'><style id='fPL3a'><dir id='fPL3a'><q id='fPL3a'></q></dir></style></legend>

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

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

                          <tbody id='fPL3a'></tbody>
                          • <bdo id='fPL3a'></bdo><ul id='fPL3a'></ul>