如何使用CDbCriteria建立类似下面的条件?
(
title LIKE :yc0
OR
description LIKE :yc0
OR
tags LIKE :yc0
)
AND
(
user=:yc1
OR
user=:yc2
)
我想只使用两种CDbcriteria方法,即compare()和addSearchCondition()
答案 0 :(得分:5)
尝试类似下面的内容。我假设你想要LIKE值周围的%
个通配符:
$Criteria = new CDbCriteria;
$Criteria->addCondition('title LIKE :value OR description LIKE :value OR tags LIKE :value');
$Criteria->addCondition('user = :user1 OR user = :user2');
$Criteria->params = array(
':value' => '%'.$value.'%',
':user1' => $user1,
':user2' => $user2,
);
$YourModels = YourModel::model()->findAll($Criteria);
或者,如果您想使用addSearchCondition
和compare
:
$value = 'foo';
$user1 = 1;
$user2 = 2;
$Criteria = new CDbCriteria;
$Criteria->addSearchCondition('title', $value, true, 'OR', 'LIKE');
$Criteria->addSearchCondition('description', $value, true, 'OR', 'LIKE');
$Criteria->addSearchCondition('tags', $value, true, 'OR', 'LIKE');
$Criteria->compare('user', array($user1, $user2));
$YourModels = YourModel::model()->findAll($Criteria);