Answer the question
In order to leave comments, you need to log in
How to get id array of related hasMany models?
Here are the models in brackets attributes Category (id, title), CatToProd (id, id_category, id_product), Product (id, title)
here are the links in the Product model
public function getCategorys(){
return $this->hasMany(Category::className(),['id'=>'id_category'])->viaTable('cat_to_prod', ['id_product' => 'id']);
}
public function getCatToProd(){
return $this->hasMany(CatToProd::className(),['id_product' => 'id']);
}
public function getDataCategory($model){
$categorys=$model->categorys;
$arrResult=array();
foreach ($categorys as $category){
$arrResult[]=$category->id;
}
return $arrResult;
}
Answer the question
In order to leave comments, you need to log in
public function getDataCategory($model){
return array_column($model->categorys, 'id');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question