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
color
is in your database tablecars
, so your classCars
must be extended byActiveRecord
. You have to just addcolor
in rules of your modelCars
. For example:The way you added it, the property will be always
null
except you set some value. If you set it in rules, when you find object, the object will havecolor
with value that is in your database. If you have some questions, give your code from classCars
andCarsSearch