I want to use a checkboxList
to show data from a data provider.
My view file:
$offices = Offices::findMyOffices();
echo Html::checkboxList('name', [], $offices);
My model file:
public static function findMyOffices()
{
$dataProvider = new ArrayDataProvider([
'allModels' => 'SELECT id_office ...'
]);
return $dataProvider;
}
But the view shows me the checkbox list with the sql query instead of the sql query's results :
I solve it using sqlDataProvider
:
View:
$offices = Offices::findMyOffices();
echo Html::checkboxList('name', [], ArrayHelper::map($offices, 'id_office', 'name_office'));
Model:
public static function findMyOffices()
{
$dataProvider = new sqlDataProvider([
'sql' => 'SELECT id_office ...'
]);
return $dataProvider->getModels();
}
ArrayDataProvider needs an array of items. you can add ->asArray() to your activequery.
$dataProvider = new ArrayDataProvider([
'allModels' => [['id' => 1, 'title' => 'xxx, ...], ...],
]);
my favorite for fetching data for a dropdown is:
MyModel::find()->select('name', 'id')->indexBy('id')->column()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.