Yii2同一模型的多个实例

时间:2014-07-19 06:32:13

标签: php yii yii-extensions yii-components yii2

我想在我的控制器中获得相同模型的multiplie实例。我看到this wiki for Yii 1.1并尝试过这样但是在我的代码中只有表格中的最后一个实例从控制器加入我的代码在这里(我用错误和变量值注释了代码):

$model = new Person(['scenario' => 'create_update']);
$contractDate = new DatePart(); // DatePart is my own class
$contractExpirationDate = new DatePart(); // DatePart is my own class

if ($model->load(Yii::$app->request->post()) &&
    $contractDate->load(Yii::$app->request->post()) &&
    $contractExpirationDate->load(Yii::$app->request->post())){

    Yii::info(Yii::$app->request->post(),'test'); // only one instance of Person and one instance of DatePart are available here
    Yii::info($_POST['DatePart'],'test'); // only last instance of DatePart (contractExpirationDate in html form) is available here
    Yii::info($_POST['DatePart'][0],'test'); // Error: Undefined offset: 0
    Yii::info($_POST['DatePart'][1],'test'); // Error: Undefined offset: 1

    $model->save();
    return $this->redirect(['view', 'id' => $model->id]);
} else {
    return $this->render('create', [
        'model' => $model,
        'contractDate' => $contractDate,
        'contractExpirationDate' => $contractExpirationDate,
    ]);
}

这是我在_form.php中的表单视图:

<?php

use yii\helpers\Html;
//use yii\widgets\ActiveForm;
use kartik\widgets\ActiveForm;
use common\models\DataOperations;

/* @var $this yii\web\View */
/* @var $model app\models\Person */
/* @var $contractDate backend\viewModels\DatePart */
/* @var $contractExpirationDate backend\viewModels\DatePart */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="user-form">
    <?php
    $form = kartik\widgets\ActiveForm::begin(
        [
            'id' => $model->isNewRecord ? 'user-form-create' : 'user-form-update',
            'type' => ActiveForm::TYPE_VERTICAL,
            //'enableAjaxValidation' => true,
            'fieldConfig' => [
                //'autoPlaceholder'=>true
            ]
        ]);
    ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 60]) ?>  
    <?= $form->field($model, 'family')->textInput(['maxlength' => 60]) ?>
    <?= $form->field($model, 'mobile')->textInput() ?>

    <?= $form->field($contractDate, 'year')->textInput() ?>
    <?= $form->field($contractDate, 'month')->textInput() ?>
    <?= $form->field($contractDate, 'day')->textInput() ?>

    <?= $form->field($contractExpirationDate, 'year')->textInput() ?>
    <?= $form->field($contractExpirationDate, 'month')->textInput() ?>
    <?= $form->field($contractExpirationDate, 'day')->textInput() ?>

    <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(); ?>  
</div>

是日志结果: 的的Yii ::信息(Yii的:: $ APP-&GT;请求 - &GT;柱()&#39;测试&#39) 在调试器中,因为您只看到最后一个DatePart可用,但我有两个DatePart模型实例(contractDate和contractExpirationDate):

[
    '_csrf' => 'Vl81R0ZvMk1hD1oELT9aDzkIe3EPHFgiIBJTBhA9RD8GbFM.AhlVBw==',
    'Person' => [
        'name' => 'test name',
        'family' => 'test family',
        'mobile' => '09121212123',
    ],
    'DatePart' => [
        'year' => '2015',
        'month' => 'Jun',
        'day' => 'Mon',
    ],
]

3 个答案:

答案 0 :(得分:12)

控制器:

    $model = new Person(['scenario' => 'create_update']);
    $dates = [
        'contractDate' => new DatePart(),
        'contractExpirationDate' => new DatePart()
    ];
    if ($model->load(Yii::$app->request->post())) {

        if (Model::loadMultiple($dates, Yii::$app->request->post()) && Model::validateMultiple($dates)) {
            foreach ($dates as $date) {
                $date->save();
            }
            // or
            $contractDate = $dates['contractDate'];
            $contractExpirationDate = $dates['contractExpirationDate'];
            // ...... some logic

            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        }
    }
    else {
            return $this->render('create', [
                'model' => $model,
                'dates' => $dates
            ]);
    }

查看表单:

<?= $form->field($dates, '[contractDate]year')->textInput() ?>
<?= $form->field($dates, '[contractDate]month')->textInput() ?>
<?= $form->field($dates, '[contractDate]day')->textInput() ?>

<?= $form->field($dates, '[contractExpirationDate]year')->textInput() ?>
<?= $form->field($dates, '[contractExpirationDate]month')->textInput() ?>
<?= $form->field($dates, '[contractExpirationDate]day')->textInput() ?>

答案 1 :(得分:5)

补充b24答案。

//在视图表单中添加foreach:

<?php foreach ($dates as $index => $date): ?>

<?= $form->field($date, '[contractDate]year')->textInput() ?>
<?= $form->field($date, '[contractDate]month')->textInput() ?>
<?= $form->field($date, '[contractDate]day')->textInput() ?>

<?= $form->field($date, '[contractExpirationDate]year')->textInput() ?>
<?= $form->field($date, '[contractExpirationDate]month')->textInput() ?>
<?= $form->field($date, '[contractExpirationDate]day')->textInput() ?>

<?php endforeach; ?>

// - 或 - 添加索引

<?= $form->field($dates['contractDate'], '[contractDate]year')->textInput() ?>
<?= $form->field($dates['contractDate'], '[contractDate]month')->textInput() ?>
<?= $form->field($dates['contractDate'], '[contractDate]day')->textInput() ?>

<?= $form->field($dates['contractExpirationDate'], '[contractExpirationDate]year')->textInput() ?>
<?= $form->field($dates['contractExpirationDate'], '[contractExpirationDate]month')->textInput() ?>
<?= $form->field($dates['contractExpirationDate'], '[contractExpirationDate]day')->textInput() ?>

答案 2 :(得分:0)