如何使用$criteria->compare()
或$criteria->addCondition()
我有一个名为product的表,在这个表中我有多个列,例如:" productId,name,description ..."
我需要我的用户能够在多个列中搜索
$criteria->compare('name' AND 'id' And '','' ,$search,true);
但这永远不会奏效!只有一列的工作方式如下:
$ criteria-> addCondition(" name LIKE'%$ search%'");
或$ criteria->比较(' productId',$ search,true);
我的搜索表单:
<div class="search-bar col-md-9 col-lg-9 no-hor-padding">
<form role="form" onSubmit="return dosearch();" class="navbar-form- navbar-left- search-form" style="padding-left: 0;"
action="<?php echo Yii::app()->createAbsoluteUrl('/'); ?>" method="get">
<input type="text" maxlength="30" placeholder="<?php echo Yii::t('app','Search products'); ?>" class="search-icon form-control input-search <?php echo !empty(Yii::app()->user->id) ? "" : "sign" ?>" name="search"></input>
</form>
</div>
&#13;
我的功能或控制器:
if(!empty($search)) {
//$criteria->addCondition("name LIKE '%$search%'");
$criteria->compare('name',$search,true);
$searchCriteria = clone $criteria;
$searchproducts = Products::model()->find($searchCriteria);
if(empty($searchproducts)){
$catrest = 1;
$location = 1;
}
}
Products :: model()参考产品表 以下是表&#34; hts_products&#34;。:
的搜索函数表单模型类
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('productId',$this->productId);
$criteria->compare('userId',$this->userId);
$criteria->compare('name',$this->name,true);
$criteria->compare('price',$this->price);
$criteria->compare('quantity',$this->quantity);
$criteria->compare('sizeOptions',$this->sizeOptions,true);
$criteria->compare('productCondition',$this->productCondition,true);
$criteria->compare("from_unixtime(`createdDate`, '%d-%m-%Y')",$this->createdDate,true);
//if(!empty($this->createdDate))
//$criteria->condition = "from_unixtime(`createdDate`, '%d-%m-%Y') = '$this->createdDate'";
//else
//$criteria->compare('createdDate',$this->createdDate);
/* $criteria->compare('likeCount',$this->likeCount);
$criteria->compare('commentCount',$this->commentCount); */
$criteria->compare('chatAndBuy',$this->chatAndBuy);
$criteria->compare('exchangeToBuy',$this->exchangeToBuy);
$criteria->compare('instantBuy',$this->instantBuy);
$criteria->compare('paypalid',$this->paypalid,true);
// $criteria->order = 'productId DESC';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>10,
),
'sort'=>array(
'defaultOrder'=>'productId DESC',
)
));
}
&#13;
感谢。
答案 0 :(得分:0)
对于多列搜索,使用变量和用户输入,compare()
可能是最佳选择。取自official Yii 1.1.x docs:
compare()方法使用新的比较附加到condition属性 表达。比较是通过比较列与 使用一些比较运算符给定值。 请注意,将从值中删除任何周围的空格 比较之前。当值为空时,没有比较表达式 将被添加到搜索条件中。
实施例。在模型Invoices
中,在提交之前,在搜索表单中包含空值:
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('invoice_number', $this->invoice_number);
$criteria->compare('city_id', $this->city_id);
$criteria->compare('state', $this->state);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
它将输出类似于:
的内容SELECT * FROM `invoices`
提交后,在搜索表单中填写所有值:
SELECT * FROM `invoices`
WHERE ( invoice_number = :ycp1 AND city_id = :ycp2 AND state = :ycp3)
仅填写invoice_number
并从表单中提交。 city_id
和state
为空:
SELECT * FROM `invoices`
WHERE ( invoice_number = :ycp1 )
您可以查看所有compare() method documentation以根据需要自定义它。
我建议您使用compare()
来匹配数字等确切值。
对于使用LIKE或不精确/近似值的多列搜索,建议您使用addSearchCondition()。
示例:
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('invoice_number', $this->invoice_number);
$criteria->compare('city_id', $this->city_id);
$criteria->compare('state', $this->state);
$criteria->addSearchCondition('customer_name', $this->customer_name);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
它将输出类似于:
的内容SELECT * FROM `invoices`
WHERE ( invoice_number = :ycp1 AND city_id = :ycp2 AND state = :ycp3 AND customer_name LIKE :ycp4 )
如果您需要使用compare()
的更多代码示例以及CDbCriteria
you can see this nice article中的其他方法。