Yii CActiveModel filter 搜索

3 篇文章 0 订阅

Yii 列表做关联搜索

单个属性的搜索,Model声明属性,Search添加条件,Controller 赋值搜索,略显繁琐

class Product extends GxActiveRecord {
	// ...
	
	// Category filter
	public $category_id;
	
	// ...
	
	public function search() {
		$criteria = new CDbCriteria;
		
		// ...
		
		// Category filter
		$criteria->compare('product2categories.category_id', $this->category_id, false);
		
		// ...
		
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			// ...
		));
	}
}

class ProductController extends GxController {

	// ...

	public function actionIndex($category_id = 0) {
		$model = new Product('search');
		$model->unsetAttributes();
		
		// ...
		
		if($category_id){
			$model->category_id = (int)$category_id;
		}
		
		// ...
		
		$this->render('index', array(
			'model' => $model,
			'category_id' => $category_id,
			// ...
		));
	}
}

// view ....

简单点就把整个关联的Model直接当成属性


class Product extends GxActiveRecord {
	// ...
	
	public $searchI18n;
	
	// ...
	
	public function search() {
		$criteria = new CDbCriteria;
		
		// ...
		
		$criteria->compare('productI18ns.title', $this->searchI18n->title, true);
		$criteria->compare('productI18ns.keywords', $this->searchI18n->keywords, true);
		$criteria->compare('productI18ns.description', $this->searchI18n->description, true);
		
		// ...
		
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			// ...
		));
	}
}

class ProductController extends GxController {

	// ...

	public function actionIndex($category_id = 0) {
		$model = new Product('search');
		$model->unsetAttributes();
		
		// ...
		
		$i18n = new ProductI18n('search');
		$i18n->unsetAttributes();

		$model->searchI18n = $i18n;
		
		// ...
		
		if (isset($_GET['ProductI18n'])){
			$i18n->setAttributes($_GET['ProductI18n']);
		}
		
		// ...
		
		$this->render('index', array(
			'model' => $model,
			'i18n' => $i18n,
			// ...
		));
	}
}

// view ....
// Yii version 1.1.14


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值