C
C
Chrome Toaster2019-12-01 15:06:39
Yii
Chrome Toaster, 2019-12-01 15:06:39

How to load data into linked yii model?

Hello.
There is a model with connection:

/**
 * This is the model class for table "{{%setting}}".
 *
 * @property int $id
 * @property string $name
 * @property int $status
 */
class Setting extends \yii\db\ActiveRecord
{
    ...
    public function getAssign()
    {
        return $this->hasOne(SettingAssign::className(), ['setting_id' => 'id']);
    }
    ...
}

Communication model:
/**
 * This is the model class for table "{{%setting_assign}}".
 *
 * @property int $id
 * @property int $setting_id
 * @property string|null $lang_code
 * @property int $value
 */
class SettingAssign extends \yii\db\ActiveRecord
{
    //модель без изменений.
}

output form:
assign - connection name (hasOne)
<?=$form->field($model, "assign[value]")->checkbox();?>
или так:
<?=$form->field($model->assign, "value")->checkbox();?>

I'm trying to load into the model and into the associated model:
public function actionIndex()
{
    $model = Setting::find()->with('assign')->where(['name' => 'disconnected'])->one();
    if( $model->load(\Yii::$app->request->post()) && $model->validate()){
        $model->save();
    }

    return $this->render('_form', ['model' => $model]); 
}

But the data is not loaded into the associated model, tell me why?
What is the correct way to load data into a linked model?
Of course, I can do this:
$model->assign->load($data);
then everything will load, but I'm not sure if this is correct.
Please advise, I will be grateful

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question