OmgDef/yii2-multilingual-behavior

Multilingual attributes empty when saving post on create

Opened this issue · 6 comments

jaaf commented

I am using "omgdef/yii2-multilingual-behavior": "^2.1" (the last version I guess)
My behavior in my Post class is as follow:

public function behaviors()
{
    return [
        'ml' => [
            'class' => MultilingualBehavior::className(),
            'languages' => [
                'fr',
                'en',
            ],
            'languageField' => 'language',
            //'localizedPrefix' => '',
            'requireTranslations' => false,
            'dynamicLangClass' => true,
            //'langClassName' => PostLang::className(), 
            // or namespace/for/a/class/PostLang
            'defaultLanguage' => 'fr',
            'langForeignKey' => 'post_id',
            'tableName' => "{{%postlang}}",
            'attributes' => [
                'title', 'content',
            ]
        ],
    ];
}

I added two fields for title and content in the _form()
Putting some spying instructions in saveTranslations() in the behavior, I can see that the $owner->$attribute values are always null for multilingal attributes.
The post table is correctly added a record but not the postlang table.
If I enter manually a record in postlang with the foreignkey pointing to a valide id in post table, when I call post/update/id the form is fully loaded (with title and content) from the DB. But after modifying the title and content (multilingual fields) and submitting, the updating doesn't happen.

Here is the create action


    /**
     * Creates a new Post model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Post();

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

Try to add to your Post model rule [['title', 'content'], 'string']

jaaf commented

Fine! It works now.

jaaf commented

The creation works. Thank to you snapget. But now I am facing a new save issue.
I use this in my controller to view the created post

    /**
     * Displays a single Post model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model=Post::find()->localized('fr')
           ->select(['post.id','enabled', 'created_at', 'updated_at','title','content'])
           -> from (['post', 'postlang'])
           -> where (['post.id' => $id])
           ->one();     
        return $this->render('view', [
            'model' => $model,//$this->findModel($id),
        ]);
    }

The view 'view' is correctly loaded with the data. Fine.

Then I use this in the controller for update action

    /**
     * Updates an existing Post model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model=Post::find()->localized('fr')
           ->select(['post.id','enabled', 'created_at', 'updated_at','title','content'])
           -> from (['post', 'postlang'])
           -> where (['post.id' => $id])
           ->one();

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

This time again data from the DB are correctly loaded into the form, but when submitting, it seems that the saving doesn't occur anew.

Try in actionUpdate

...
$model=Post::find()->multilingual()
           ->select(['post.id','enabled', 'created_at', 'updated_at','title','content'])
           -> from (['post', 'postlang'])
           -> where (['post.id' => $id])
           ->one();
...

and in form view

...
<?php echo  $form->field($model, 'title')->textInput()  ?>
<?php echo  $form->field($model, 'title_en')->textInput()  ?>
<?php echo  $form->field($model, 'content')->textInput()  ?>
<?php echo  $form->field($model, 'content_en')->textInput()  ?>
...

in this way you can set values for both languages

jaaf commented

Thank you very much. It works! I put all the input fields into a _form.php that is common to both create and update. That way I can create and update every/any thing. But it can become cumbersome if there are multiple huge textareas and more languages, thus I must organise the fields in a tabbed view.
Nevertheless, I am still wondering why I cannot get and update only one language at a time.

The same question: why it's not possible to get and update record using "localized()" ?