我有全名自动完成的问题。
我有一个用户表,其中包含名字、姓氏列。通过 ajax。
在搜索操作中,这是我的代码:
$users = (new Query())
->select('*')
->from($userTable)
->where(['like', 'username', $searchTerm])
->orWhere(['like', 'firstname', $searchTerm])
->orWhere(['like','lastname', $searchTerm])
->andWhere(['<>','id', Yii::$app->user->id])
->andWhere(['status'=>self::STATUS_ACTIVE])
->orderBy('username')
->limit(20)
->all();
我的问题是当我尝试使用姓氏搜索名字时获得空记录。
感谢您的支持,我通过保持 concat 解决了
$users = (new Query())
->select('*')
->from($userTable)
->where(['like', 'username', $searchTerm])
->orWhere(['like', "CONCAT(firstname, ' ', lastname)", $searchTerm])
->andWhere(['<>','id', Yii::$app->user->id])
->andWhere(['status'=>self::STATUS_ACTIVE])
->orderBy('username')
->limit(20)
->all();
最佳答案
以下代码对我来说使用 SQL 数据库工作正常
$query->orWhere(['like', "CONCAT([staff].[first_name], ' ',[staff].[middle_name],' ', [staff].[last_name])", $this->name]);
关于php - Yii2 where 条件与 concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38145803/