okokad 2016-08-06 12:31:49 12668次浏览 4条评论 7 0 0

GII后

控制器里里加:

use yii\base\Model;

public function actionCreate()
{   
    $model = new Test;
    $ad = new Ad;
    if ($model->load(Yii::$app->request->post()) && $ad->load(Yii::$app->request->post()) && Model::validateMultiple([$model, $ad])) {
        $model->save(false); // skip validation as model is already validated
        //$ad->user_id = $user->id; // no need for validation rule on user_id as you set it yourself
        $ad->save(false); 
        //return $this->redirect(['view', 'id' => $user->id]);
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'ad' => $ad,
        ]);
    }
}

视图:
……

<?= $form->field($model, 'content')->textInput(['maxlength' => true]) ?>

<?= $form->field($ad, 'name')->textInput() ?>

……

您需要登录后才可以评论。登录 | 立即注册