Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multilingual attributes empty when saving post on create #54

Open
jaaf opened this issue Sep 20, 2016 · 6 comments
Open

Multilingual attributes empty when saving post on create #54

jaaf opened this issue Sep 20, 2016 · 6 comments

Comments

@jaaf
Copy link

jaaf commented Sep 20, 2016

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,
            ]);
        }
    }
@vasyl-dmytruk
Copy link

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

@jaaf
Copy link
Author

jaaf commented Sep 20, 2016

Fine! It works now.

@jaaf jaaf closed this as completed Sep 20, 2016
@jaaf jaaf reopened this Sep 20, 2016
@jaaf
Copy link
Author

jaaf commented Sep 20, 2016

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.

@vasyl-dmytruk
Copy link

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
Copy link
Author

jaaf commented Sep 20, 2016

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.

@vadim-bulochnik
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants