cruim
cruim

Reputation: 319

foreach in gridview columns yii2

I have relations in main model

public function getItems(){
   return $this->hasOne(OrderItems::className(), ['order_id' => 'order_id']);
}

In Items order_id has many order_items_id. How could I get all order_items_id for order_id in one cell in column?

Upvotes: 2

Views: 530

Answers (1)

Fabrizio Caldarelli
Fabrizio Caldarelli

Reputation: 3008

The relation should be with hasMany and not hasOne

public function getItems(){
   return $this->hasOne(OrderItems::className(), ['order_id' => 'order_id']);
}

To solve your problem I'll try with raw sql, using GROUP_CONCAT:

$sql = "SELECT GROUP_CONCAT(order_items_id SEPARATOR ', ') FROM OrderItems";
$orderItemsIds = Yii::$app->db->createCommand($sql)->queryColumn();

Upvotes: 1

Related Questions