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传递给小部件时的管理页面截图,a当我像上面的代码一样通过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',
),
),
)); ?>
相关文章