2016-04-17 37 views
1

Ich versuche mehrsprachige Inhalte im Modell zu speichern. Ich habe zwei ModelleWie kann ich Inhalte speichern?

PostLang

class PostLang extends \yii\db\ActiveRecord 
     { 

public static function tableName() 
{ 
    return 'postlang'; 
} 

public function rules() 
{ 
    return [ 
     [['post_id', 'lang_id', 'title', 'content'], 'safe'], 
     [['post_id', 'lang_id'], 'integer'], 
     [['title', 'content'], 'string'], 
     [['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['post_id' => 'id']], 
    ]; 
} 
... 
    public function getPost() 
{ 
    return $this->hasOne(Post::className(), ['id' => 'post_id']); 
} 
    } 

und Post

class Post extends \yii\db\ActiveRecord { 
     public static function tableName() { 
    return 'post'; 
} 

public function behaviors() { 
    return [ 
     'timestamp' => [ 
      'class' => 'yii\behaviors\TimestampBehavior', 
      'attributes' => [ 
       \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['date_create', 'date_update'], 
       \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['date_update'], 
      ], 
     ], 
    ]; 
} 


public function rules() { 
    return [ 
     [['status', 'date_update', 'date_create'], 'integer'], 
     [['date_update', 'date_create'], 'safe'], 
    ]; 
} 
    ... 
public function getPostlangs() { 
    return $this->hasMany(Postlang::className(), ['post_id' => 'id']); 
} 

}

habe ich eine Postcontroller mit create-Methode

public function actionCreate() { 
    $model = new Post(); 
    $post_ru = new PostLang(); 
    $post_en = new PostLang(); 

    if ($model->load(Yii::$app->request->post())) { 
     if ($model->save()) { 

      $dbPost = new PostLang(); 
      $dbPost->title = need to save title; 
      $dbPost->content = need to save content; 
      $dbPost->lang_id = need to save lang_id; 
      $dbPost->post_id = $model->id; 
      $dbPost->save(); 
     } 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

Ich brauche es zu speichern in für dich ch aber ich verstehe nicht, wie kann ich es tun. Formular

... 
<?= $form->field($post_ru, 'title')->textInput() ?> 

<?= $form->field($post_ru, 'content')->textInput() ?> 

<?= $form->field($post_en, 'title')->textInput() ?> 

<?= $form->field($post_en, 'content')->textInput() ?> 

...

Antwort

1

Sie sollen Modelle in Ihrer Active trennen, denn nur letztes Modell sparen.

Form:

<?= $form->field($post_ru, "[0]title")->textInput() ?> 

<?= $form->field($post_ru, "[0]content")->textInput() ?> 

<?= $form->field($post_en, "[1]title")->textInput() ?> 

<?= $form->field($post_en, "[1]content")->textInput() ?> 

Controller:

public function actionCreate() 
{ 
    $model = new Post(); 
    $post_ru = new PostLang(); 
    $post_en = new PostLang(); 
    $postData = Yii::$app->request->post('PostLang'); 
    if ($model->load(Yii::$app->request->post())) { 
     if ($model->save()) { 
      $post_ru->load($postData[0]); 
      $post_en->load($postData[1]); 
      if ($post_ru->save()) { 
       $post_ru->link('post', $model); 
      } 
      if ($post_en->save()) { 
       $post_en->link('post', $model); 
      } 
     } 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

Don`t bis + rep vergessen, wenn es sinnvoll war.

+0

Fehler bei 'if ($ post_ru-> save()) {'" Integritätseinschränkung: 1452 Hinzufügen oder Aktualisieren einer untergeordneten Zeile: Eine Fremdschlüsseleinschränkung schlägt fehl ('asur'.postlang', CONSTRAINT' lang-to-post 'FOREIGN KEY (' post_id') REFERENZEN 'post' (' id') ON DELETE CASCADE AUF UPDATE CASCADE) " – Slip

+0

Gehe zur yii2 Debug-Symbolleiste und wähle db tab. Es gibt sql Anfrage, die fehlschlägt.Senden Sie mich, ich werde überprüfen. –

Verwandte Themen