php - Yii2 where 条件与 concat

标签 php yii2

我有全名自动完成的问题。
我有一个用户表,其中包含名字、姓氏列。通过 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/

相关文章:

javascript - 根据 Internet 可用性从在线样式表切换到离线样式表

php - yii2 POST参数映射

php - yii2 如何防止 swiftmailer 将电子邮件发送到垃圾邮件文件夹

mysql - 在 yii2 的 $query 中添加 beetween 选项

php - 如何在 Yii2 中为 cookie 设置纯值(无 json)

PHP5 : Callbacks between Class Objects

php - 正则表达式查找所有 URL 和标题

javascript - Sweet Alerts 显示 UTF-8 字符错误

php - 深入解释 Magento 的 Store/Store View/Website 层?

php - Yii2 中的 block 是什么