Yii2以多种模式保存表格 [英] Yii2 saving form with multiple models

查看:199
本文介绍了Yii2以多种模式保存表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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





$在表单中我有3个主要字段进行更新或者在不记录的情况下我需要创建一个新记录。



这是_form-today-attendance

 <?phpuse yii \helpers\Html;使用yii\widgets\ActiveForm;使用yii\helpers\ArrayHelper;?><?php $ form = ActiveForm :: begin(); ?>< h3>您的个人资料< / h3><?= $ form->栏位($ modelCustomers,'Name') - > textInput(['readonly'=> true])?> <?= $ form-> field($ model,'DropinDate') - > textInput(['readonly'=> true])?>< div class =attendance-form> <?= $ form->字段($ model,'Dropin') - > checkbox()?> <?= $ form->字段($ model,'Doctor') - > checkbox()?> <?= $ form->字段($ model,'律师') - > checkbox()?> <?= $ form->字段($ 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(); ?>  



当我调试时,



任何想法?
$ b

Eduardo

解决方案

试试这个函数并检查你得到的结果。
$ b

  public function actionToday()
{
$ ID = $ _GET [0];
$ modelCustomers = Customers :: find()
- >其中(['ID'=> $ ID])
- > one();;
$ today = date(Y-m-d);
$ beforeToday ='DropinDate>'。 $今天;
$ modelAttendance = Attendance :: find()
- >其中(['CustomersID'=> $ ID])
- >和其中(['DropinDate'=> $今天])
- > one(); ();
if(Yii :: $ app-> request-> post()){
$ data = Yii :: $ app-> request-> post();
//用$ data
}
做某事返回$ this-> render('//考勤/ _form-today-attendance',[
'modelCustomers'=> ; $ modelCustomers,
'model'=> $ modelAttendance]);
}

数组中会有一些内容,您可以将其分配给模型实例。 / p>

Hi I am very close to finish a project but got stuck saving a from with multiple models.

I have a grid that calls a controllers action that calls a 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 to update or in case is not record i need to create a new record.

This is the _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(); ?>

When I debug i cant get anything happend in the Attendence model or the Customers model.

Any ideas?

Thanks a lot,

Eduardo

解决方案

Try this function and check 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 array, you can assign it to model instances.

这篇关于Yii2以多种模式保存表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆