Yii2保存形式有多种型号

时间:2015-02-24 19:01:25

标签: forms grid yii2 yii-extensions

嗨,我非常接近完成一个项目,但因多个模型而无法保存。

我有一个网格,它调用一个调用表单的控制器动作。

    public function actionToday() {
    $ID = $_GET["0"];
    $modelCustomers = Customers::find()->where(['ID' => $ID])->one();;
    $today = date("Y-m-d");
    $beforeToday = 'DropinDate>'.$today;
    $modelAttendance =  Attendance::find()->where(['CustomersID' => $ID])->andwhere(['DropinDate' => $today])->one();
    return $this->render('//attendance/_form-today-attendance', ['modelCustomers' => $modelCustomers, 'model' => $modelAttendance]);
}  

在表单中我有3个主要字段需要更新,或者如果不是记录,我需要创建一个新记录。

这是_form-today-attendance

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
?>
<?php $form = ActiveForm::begin(); ?>
<h3>Your Personal Details</h3>
<?= $form->field($modelCustomers, 'Name')->textInput(['readonly' => true]) ?>
<?= $form->field($model, 'DropinDate')->textInput(['readonly' => true]) ?>

<div class="attendance-form">
    <?= $form->field($model, 'Dropin')->checkbox() ?>
    <?= $form->field($model, 'Doctor')->checkbox() ?>
    <?= $form->field($model, 'Lawyer')->checkbox() ?>
    <?= $form->field($model, 'Observation')->textInput(['maxlength' => 250]) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
</div>
<?php ActiveForm::end(); ?>

当我调试时,我无法在Attendence模型或Customers模型中发生任何事情。

有什么想法吗?

非常感谢,

爱德华

1 个答案:

答案 0 :(得分:3)

尝试此功能并检查你得到了什么。

public function actionToday()
{
    $ID = $_GET["0"];
    $modelCustomers = Customers::find()
        ->where(['ID' => $ID])
        ->one();;
    $today = date("Y-m-d");
    $beforeToday = 'DropinDate>' . $today;
    $modelAttendance = Attendance::find()
        ->where(['CustomersID' => $ID])
        ->andwhere(['DropinDate' => $today])
        ->one();
    if (Yii::$app->request->post()) {
        $data = Yii::$app->request->post();
        //do something with $data
    }
    return $this->render('//attendance/_form-today-attendance', [
        'modelCustomers' => $modelCustomers,
        'model' => $modelAttendance]);
}

数组中会有一些东西,您可以将它分配给模型实例。