S
S
shanik2017-09-10 11:26:18
PHP
shanik, 2017-09-10 11:26:18

How to select checkbox when changing data Yii2?

There is a form

<?=$f->field($flora_user, 'user_id')->checkBoxList(ArrayHelper::map($user,'id','fio'))->label('Автор(ы):')?>

$floraUser = new floraUser();
$user = User::find()->all();

floraUser is an intermediate table
When opening a form for changing data, it is necessary to mark the necessary checkboxes that were marked when adding.
If you do this, then you get an array and the error Call to a member function isAttributeRequired() on array
$floraUser = $this->findModel($id)->users;
findModel($id)
    {
        if (($model = Flora::findOne($id)) !== null) {
            return $model;
}

connections
public function getFloraUsers()
    {
        return $this->hasMany(FloraUser::className(), ['flora_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('flora_user', ['flora_id' => 'id']);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Immortal_pony, 2019-05-22
@ArtJH

exec will just execute the query, use getAll/getRow/getCol/getCell/getAssoc to get the results.

B
Boris Korobkov, 2017-09-10
@shanik

<?= $f->field($flora, 'users')
->checkBoxList(User::find()->select(['name', 'id'])->indexBy('id')->column())
->label('Автор(ы):') ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question