OmgDef/yii2-multilingual-behavior

Language table empty when creating model

Opened this issue · 0 comments

Data not saved in language table.

CREATE TABLE category (
id int(3) unsigned NOT NULL AUTO_INCREMENT,
parent_id int(3) unsigned NOT NULL DEFAULT '0',
slug char(30) NOT NULL,
position int(3) unsigned DEFAULT '999',
status smallint(1) unsigned DEFAULT '1',
created_at bigint(20) unsigned NOT NULL,
updated_at bigint(20) unsigned NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY slug (slug)
);

CREATE TABLE category_locale (
category_id int(3) unsigned NOT NULL,
language_code char(3) NOT NULL,
name char(45) NOT NULL,
status smallint(1) unsigned NOT NULL DEFAULT '1',
description text,
meta_title char(70) NOT NULL,
meta_description char(160) NOT NULL,
created_at bigint(20) unsigned NOT NULL,
updated_at bigint(20) unsigned NOT NULL,
PRIMARY KEY (category_id,language_code),
KEY FK_CL_language (language_code),
CONSTRAINT FK_CL_category FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_CL_language FOREIGN KEY (language_code) REFERENCES language (iso_code) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE language (
id int(3) unsigned NOT NULL AUTO_INCREMENT,
name char(32) NOT NULL,
iso_code char(3) NOT NULL,
language_code char(5) NOT NULL,
status smallint(1) unsigned NOT NULL DEFAULT '1',
is_default smallint(1) unsigned NOT NULL DEFAULT '0',
created_at bigint(20) unsigned NOT NULL,
updated_at bigint(20) unsigned NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY iso_code (iso_code)
);

'ml' => [
'class' => MultilingualBehavior::className(),
'languages' => [
'en-IN' => 'English'
],
'langClassName' => CategoryLocale::className(),
'langForeignKey' => 'category_id',
'tableName' => "{{%category_locale}}",
'languageField' => 'language_code',
'requireTranslations' => true,
'attributes' => [
'name',
'description',
'meta_title',
'meta_description'
]
]

Below is my form :-

<?php $form = ActiveForm::begin(); ?>

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

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

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

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

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

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

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

<?= $form->field($model, 'status')->checkbox() ?>


<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

Below is my controller code 👎

public function actionUpdate( $id ) {
$model = $this->findModel( $id, true );
print_r($model->save());
if ( $model->load( Yii::$app->request->post() ) && $model->save() ) {
echo "here";
return $this->redirect( [ 'view', 'id' => $model->id ] );
} else {
return $this->render( 'update', [
'model' => $model,
] );
}
}

protected function findModel( $id, $ml = false ) {
if ($ml) {
$model = Category::find()->where([ 'id' => $id ])->multilingual()->one();
} else {
$model = Category::findOne( $id );
}
if ( $model !== null ) {
return $model;
} else {
throw new NotFoundHttpException( 'The requested page does not exist.' );
}
}