我试图连接" firstname"和"姓氏"喜欢这个"名字姓氏" 在进行搜索查询时。
这是我的代码:
$query = Contact::find()
->select([
"CONCAT_WS(' ', firstname, lastname) AS name"
])
->where('(firstname like :fname) OR (lastname like :lname) OR (email = :email)', [
':fname' => $searchkey,
':lname' => $searchkey,
':email' => $searchkey,
])->all();
但结果是空的,如下:
Array
(
[0] => common\models\contact\Contact Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
)
我希望我能有类似的东西
[name] => "firstname lastname"
输出但没有运气。需要帮助谢谢。
答案 0 :(得分:1)
您应该在Contcat模型中添加公共var $ name ,并在规则中添加属性为安全,以便使用别名来重新计算值
class Contact extends \yii\db\ActiveRecord
{
public $name;
....
}