I have a find function in my CarsSearch model:
public function search($params)
{
$query = Cars::find()->select(['color' => '1']); // This is a longer SQL query.
$dataProvider = new ActiveDataProvider([
$query => $query,
$this->load($params);
return $dataProvider;
]);
}
I use that $dataProvider in my very simple cars view:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'color'
]
]) ?>
But I got this error:
Getting unknown property: app\models\Cars::color
How can I add the color property?
I see that property
coloris in your database tablecars, so your classCarsmust be extended byActiveRecord. You have to just addcolorin rules of your modelCars. For example:The way you added it, the property will be always
nullexcept you set some value. If you set it in rules, when you find object, the object will havecolorwith value that is in your database. If you have some questions, give your code from classCarsandCarsSearch