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

2023-08-17php开发问题
6

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17