Yii2 save form with multiple models

Hi, I am very close to completing the project, but am stuck in saving time with a few models.

I have a grid that calls the action of the controllers, which calls the form.

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

In the form, I have 3 main fields for updating, or if there is no record, I need to create a new record.

This is _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(); ?>
Run codeHide result

When I debug, I cannot get anything in the Attendence or Customer model.

Any ideas?

Many thanks,

Eduardo

+4
source share
1 answer

Try this feature and see what you get.

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

There will be something in the array, you can assign it to model instances.

+3

All Articles