为什么我的查询不能使用 ActiveRecord 工作?

Why does my query not work using ActiveRecord?

我尝试使用此代码获取 Article 模型:

$criteria = new CDbCriteria();
$criteria->addCondition("type = :type and name ILIKE :name";
$criteria->params = [
    ":type" => 1,
    ":name" => "art"
];
$articles = Articles::model()->findAll($criteria);

但它不起作用我得到:

database error near ILIKE.

我认为您使用了错误的 CDbCriteria 查询方法。尝试对 ILIKE

使用 addSearchCondition
$criteria->addSearchCondition("name", "art", true, "AND", "ILIKE");

您应该在搜索值中使用 % 通配符:

$criteria = new CDbCriteria();
$criteria->addCondition("type = :type and name ILIKE :name");
$criteria->params = [
    ":type" => 1,
    ":name" => '%art%',
];
$articles = Articles::model()->findAll($criteria);