OmgDef / yii2-multilingual-behavior

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

Language table empty when creating model #58

Open vikashgirnar opened 7 years ago

vikashgirnar commented 7 years ago

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 :-

field($model, 'parent_id')->textInput() ?> field($model, 'position')->textInput() ?> field($model, 'name_en')->textInput() ?> field($model, 'slug')->textInput() ?> field($model, 'description_en')->textInput() ?> field($model, 'meta_title_en')->textInput() ?> field($model, 'meta_description_en')->textInput() ?> field($model, 'status')->checkbox() ?>
isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

Below is my controller code :-1:

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.' ); } }