它不返回标题。 那么我可以在查询中更改什么呢?
$query = (new Query())->select('title')->from('topics')->where(['id' => [1, 2, 3]]);
return $query->title;
答案 0 :(得分:9)
您的查询形成类似 -
SELECT title FROM topics WHERE id IN (1,2,3);
所以你将获得数组数组。您还需要执行查询。
尝试 -
$query = (new \yii\db\Query())->select(['title'])->from('topics')->where(['id' => [1, 2, 3]]);
$command = $query->createCommand();
$data = $command->queryAll();
$titles = '';
foreach($data as $row) {
$titles .= $row['title'] . ', ';
}
return rtrim($titles, ', ');
每个记录都会得到title
,逗号分隔。
答案 1 :(得分:4)
试试这段代码:
$users=[1,2,3];
User::find()->where('id IN('.$users.')');
答案 2 :(得分:1)
[
'attribute' => 'topic_id',
'format' => 'raw',
'value' => function($data){
$query = (new Query())->select(['title'])->from('topics')->where(['id' => [1, 2, 3]]);
$command = $query->createCommand();
$data= $command->queryAll();
foreach($data as $row)
return $data['title'];
}
],
m把这段代码放在这里但是错误产生了这个。
未定义的索引:标题
标题可用于主题表。