I have a Jobs model that hasMany JobItems. Is it possible to fetch the related JobItems in the Jobs view and display, for example, the JobItem description and its related Status in one of the columns of my Job view’s gridview?
I know the syntax for getting the info in straight PHP can be done with a foreach ($model->JobItems as $JobItem) and then echoing, say, $JobItem->product->status to get the value, but I can’t quite wrap my brain around how this could be done in a gridview.
Hi, thanks so much for the guidance - it’s working, but both ways of doing this are resulting in the job items being echoed prior to the gridview being rendered.
Inside the cell where the items should be appearing, the gridview is rendering the “not set” text (probably because it’s registering null values).
Here’s my code in the model:
public function getJobDetails() {
if (is_array($this->jobItems)) {
foreach ($this->jobItems as $jobItem) {
echo $jobItem->product->product . ' - ' . $jobItem->upholstery->upholstery."<br>";
}
} else {
return Yii::$app->formatter->nullDisplay;
}
}
And in the view (both ways you suggested, they both do the exact same thing)