2015-02-24 54 views
4

嗨,我很接近完成一個項目,但卡住了保存多個型號。Yii2多種型號保存表格

我有一個網格調用一個控制器操作,調用一個窗體。

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的日常考勤

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

當我調試,我不能得到任何東西在考勤模式或客戶模式happend。

任何想法?

非常感謝,

愛德華

回答

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

將會有數組中的東西,您可以將它分配給模型實例。

+0

它使我意識到,傳遞迴相同的功能,我可以在那裏得到邏輯。謝謝,我一點一點地理解yii – 2015-02-26 10:51:40

+0

的歡迎的邏輯,也答案 – ankitr 2015-02-26 11:16:30

+0

好的@ankitraturi。 – 2015-10-19 14:34:12