Yii - how can I search by a column from foreign/re

2019-02-11 03:55发布

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

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,
    ));
}

}

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.

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.

Any ideas? Thanks.

2条回答
你好瞎i
2楼-- · 2019-02-11 04:08

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',
        ),  
    ),  
)); ?>
查看更多
Bombasti
3楼-- · 2019-02-11 04:12

It also works if you just use 'manufacturer_search' for the name of the column, as shown in the tutorial you mentioned above.

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'plastic-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'name',
        array(
            'name'=>'manufacturer_search',
            'value'=>'$data->manufacturer->name'
        ),
        array(
            'class'=>'CButtonColumn',
        ),  
    ),  
)); ?>
查看更多
登录 后发表回答