如何在 Yii 查询构建器中使用 LIKE

2022-01-04 00:00:00 mysql yii query-builder

如何使用 YIi 查询构建器构建此查询

How build this query with YIi query builder

SELECT *
FROM `table`
WHERE type_item = 2 AND name_item LIke '%name%'

我试过了

return Yii::app()->db->createCommand()
    ->select('*')
    ->from('{{event_field_variants}}')
    ->where('type_item = :type AND name_item LIKE "%:substr%"', array(':type' => '2', ':substr' => 'name'))
    ->order('variant ASC')
    ->queryAll();

但是这个查询得到 CdbcException.Yii 文档中的示例仅包含 like 或仅包含简单参数.

But this query get CdbcException. On Yii documentation are examples only with like or only with simple param.

推荐答案

->where('name_item LIKE :substr', array(':substr' => '%name%'))

把它放在变量部分?

相关文章