OmgDef / yii2-multilingual-behavior

Yii2 port of the yii-multilingual-behavior.
146 stars 60 forks source link

Save relative model #63

Open virtoso opened 7 years ago

virtoso commented 7 years ago

Hi! I have Post, Seo and PostLang models. I render my Post model like that

<?= $form->field($model, 'title')->textInput() ?>
<?= $form->field($model, 'title_en')->textInput() ?>
<?= $form->field($model, 'title_fr')->textInput() ?>

And it's saved in PostLang model. But now I want to save relation Seo model. Please can you suggest me how I can do it?

OmgDef commented 7 years ago

@virtoso Hi! Is Seo model linked to the PostLang or to the Post model?

virtoso commented 7 years ago

@OmgDef yep. I added field seo_id in PostLang and added foreign key to id in seo table

OmgDef commented 7 years ago

@virtoso There are two options. You can do it directly in you controller actions or in the afterSave callback of the PostLang model.

<?= $form->field($model, 'title')->textInput() ?>
<?= $form->field($model, 'title_en')->textInput() ?>
<?= $form->field($model, 'title_fr')->textInput() ?>
<?= $form->field($seoModels['en'], 'someAttribute')->textInput() ?>
virtoso commented 7 years ago

@OmgDef I don't need set ml behavior in the Seo model. Right?

virtoso commented 7 years ago

@OmgDef in my PostController

    public function actionCreate()
    {
        $model = new Post();
        $seo = new Seo();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'seo' => $seo,
                'languages' => Languages::getAll(),
            ]);
        }
    }

In my view

<?= $form->field($model, 'title_uk')->textInput() ?>

<hr>SEO
<?= $form->field($seo['uk'], 'title')->textInput() ?>

After that I getting an error

Getting unknown property: common\models\Seo::uk

OmgDef commented 7 years ago

@virtoso Not exactly.

In your PostController


    public function actionCreate()
    {
        $model = new Post();
        $seo = [
            'fr' => new Seo(),
            'uk' => new Seo(), 
       ]; // this one should be dynamic 

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'seo' => $seo,
                'languages' => Languages::getAll(),
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $seo = [];
        foreach($model->translations as $translation) {
              $seo[$translation->yourLanguageAttributeHere] = $translation->seo;
        }

        if (empty($seo) {
           $seo = [
              'fr' => new Seo(),
              'uk' => new Seo(), 
           ]; // this one should be dynamic 
       }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'seo' => $seo,
                'languages' => Languages::getAll(),
            ]);
        }
    }
<?= $form->field($model, 'title_uk')->textInput() ?>
SEO
<?= $form->field($seo['uk'], '[uk]title')->textInput() //it would be easier to load data from post ?>

I haven't checked it. Just wrote it here, on Github.

virtoso commented 7 years ago

@OmgDef I guess afterSave is not a right way for my task because PostLang linked with Seo by fk. So I can't save PostLang before save Seo model. First of all I have to save Seo model and get $seo->id. And it's write in PostLang model. But I can't understand how I can to do it. Or I do everything wrong :-)

OmgDef commented 7 years ago

@virtoso Oh, I don't know the whole idea, but I would link Seo to PostLang like (Seo)translation_id -> id (PostLang).

virtoso commented 7 years ago

@OmgDef Your proposal is a save PostLang ID in the SEO model? Right?