如何保存内容?

时间:2016-04-17 11:10:54

标签: php yii yii2

我尝试在模型中保存多语言内容。我有两个型号

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

和发布

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

}

我使用create method

创建了一个PostController
 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,
        ]);
    }
}

我需要将它保存在foreach中,但我不明白我该怎么做。 表格

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

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

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

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

...

1 个答案:

答案 0 :(得分:1)

您应该在activeForm中分离模型,因为只有最后一个模型会保存。

形式:

<?= $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() ?>

控制器:

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,
        ]);
    }
}

如果有用的话,别忘了+ rep。