Use select where in the request in yii2


Does not return a title. So what can I change in the request?

$query = (new Query())->select('title')->from('topics')->where(['id' => [1, 2, 3]]); return $query->title; 
+6
source share
3 answers

Your request is something like this:

 SELECT title FROM topics WHERE id IN (1,2,3); 

So you get an array of array. You also need to complete the request.

Try it -

 $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, ', '); 

You will get a title for each entry, separated by a comma.

+6
source

Try this code:

 $users=[1,2,3]; User::find()->where('id IN('.$users.')'); 
+2
source
  [ '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 puts this code, but generates this message here.

Undefined index: title

Headline

available for the topic table.

0
source

Source: https://habr.com/ru/post/975465/


All Articles