一个表单中的多个模型,与Select2具有一对多关系

时间:2018-02-03 16:41:41

标签: php yii2

我的数据库关系如下:

Relation

我的目标是,当我在bukti_penerimaan表中插入记录时,我还将大量记录保存到bukti_penerimaan_invoice表中,并在bukti_penerimaan_debit_note表中保存了大量记录。

模型

<?php
    class BuktiPenerimaan extends \yii\db\ActiveRecord{

        public function getBuktiPenerimaanDebitNotes(){
            return $this->hasMany(BuktiPenerimaanDebitNote::className(), ['bukti_penerimaan_id' => 'id']);
        }

        public function getBuktiPenerimaanInvoices(){
            return $this->hasMany(BuktiPenerimaanInvoice::className(), ['bukti_penerimaan_id' => 'id']);
        }
    }
?>

所以这就是actionCreate,它已成功运作。

控制器

public function actionCreate(){
    $request = Yii::$app->request;
    $model = new BuktiPenerimaan(); // one record
    $modelsBuktiPenerimaanInvoice = new BuktiPenerimaanInvoice(); // to many record
    $modelsBuktiPenerimaanDebitNote = new BuktiPenerimaanDebitNote(); // to many record

    if ($request->isAjax) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        if ($request->isGet) { // which is fine
            return [
                'title' => "Create new BuktiPenerimaan",
                'content' => $this->renderAjax('create', [
                    'model' => $model,
                    'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
                    'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote
                ]),
                'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])

            ];
        } else if ($model->load($request->post())&& $modelsBuktiPenerimaanDebitNote->load($request->post())&& $modelsBuktiPenerimaanInvoice->load($request->post())
                    && Model::validateMultiple([$model, $modelsBuktiPenerimaanDebitNote, $modelsBuktiPenerimaanInvoice])) {

            //doing transaction here. ;

        } else{
            return [
                'title' => "Create new BuktiPenerimaan",
                'content' => $this->renderAjax('create', [
                    'model' => $model,
                    'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
                    'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote
                ]),
                'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])

            ];
        }
    }
}

查看:create.php

    <?= $this->render('_form', [
        'model' => $model,
        'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
        'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote
]) ?>

查看_form.php

<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'rekening')->textInput(['readonly' => true, 'maxlength' => true]) ?>

    /*Find standard invoices*/
    <?php $dataInvoice = \yii\helpers\ArrayHelper::map(\app\models\finance\Invoice::find()->all(), 'id', 'nomor_surat'); ?>
    <?= $form->field($modelsBuktiPenerimaanInvoice, 'invoice_id')->widget(Select2::className(), [
        'data' => $dataInvoice,
        'options' => ['multiple' => true, 'placeholder' => 'Select Invoice / Invoices ...']
        ]); ?>

    /*Find standard debit_notes*/
    <?php $dataDebitNote = \yii\helpers\ArrayHelper::map(\app\models\finance\DebitNote::find()->all(), 'id', 'nomor_surat'); ?>
    <?= $form->field($modelsBuktiPenerimaanDebitNote, 'debit_note_id')->widget(Select2::className(), [
        'data' => $dataDebitNote,
        'options' => ['multiple' => true, 'placeholder' => 'Select Debit Note / Debit Notes ...']
    ]); ?>
<?php ActiveForm::end(); ?>

一切都很好。

但是在actionUpdate中,我收到了一个错误。

控制器

<?php   
public function actionUpdate($id){
    $request = Yii::$app->request;
    try {
        $model = $this->findModel($id);
    } catch (NotFoundHttpException $e) {
        return $e->getMessage();
    }

    $modelsBuktiPenerimaanInvoice = $model->buktiPenerimaanInvoices;
    $modelsBuktiPenerimaanDebitNote = $model->buktiPenerimaanDebitNotes;

    if ($request->isAjax) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        if ($request->isGet) {
            return [
                'title' => "Update BuktiPenerimaan #" . $id,
                'content' => $this->renderAjax('update', [
                    'model' => $model,
                    'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote,
                    'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
                ]),
                'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
            ];
        } else if ($model->load($request->post())
            && $modelsBuktiPenerimaanDebitNote->load($request->post())
            && $modelsBuktiPenerimaanInvoice->load($request->post())
            && Model::validateMultiple([$model, $modelsBuktiPenerimaanDebitNote, $modelsBuktiPenerimaanInvoice])) {
            return [
                'forceReload' => '#crud-datatable-pjax',
                'title' => "BuktiPenerimaan #" . $id,
                'content' => $this->renderAjax('view', [
                    'model' => $model,
                ]),
                'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::a('Edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
            ];
        } else {
            return [
                'title' => "Update BuktiPenerimaan #" . $id,
                'content' => $this->renderAjax('update', [
                    'model' => $model,
                    'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote,
                    'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
                ]),
                'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                    Html::button('Save', ['class' => 'btn btn-primary', 'type' => "submit"])
            ];
        }
    }
}
?>

错误是

"name":"PHP Fatal Error","message":"Call to a member function isAttributeRequired() on array","code":1,"type":"yii\\base\\ErrorException"

问题在于:

    <?php $dataInvoice = \yii\helpers\ArrayHelper::map(\app\models\finance\Invoice::find()->all(), 'id', 'nomor_surat'); ?>
<?= $form->field($modelsBuktiPenerimaanInvoice, 'invoice_id')->widget(Select2::className(), [
    'data' => $dataInvoice,
    'options' => ['multiple' => true, 'placeholder' => 'Select Invoice / Invoices ...']
]); ?>

<?php $dataDebitNote = \yii\helpers\ArrayHelper::map(\app\models\finance\DebitNote::find()->all(), 'id', 'nomor_surat'); ?>
<?= $form->field($modelsBuktiPenerimaanDebitNote, 'debit_note_id')->widget(Select2::className(), [
    'data' => $dataDebitNote,
    'options' => ['multiple' => true, 'placeholder' => 'Select Debit Note / Debit Notes ...']
]); ?>

Yii2如何将其识别为一对多关系。

更新

这是actionFindModel的样子:

protected function findModel($id)
{
    if (($model = BuktiPenerimaan::findOne($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}

请建议。

1 个答案:

答案 0 :(得分:1)

在“BuktiPenerimaan”模型中,您定义了两个一对多关系:“getBuktiPenerimaanDebitNotes”和“getBuktiPenerimaanInvoices”。

public function getBuktiPenerimaanDebitNotes(){
            return $this->hasMany(BuktiPenerimaanDebitNote::className(), ['bukti_penerimaan_id' => 'id']);
        }

        public function getBuktiPenerimaanInvoices(){
            return $this->hasMany(BuktiPenerimaanInvoice::className(), ['bukti_penerimaan_id' => 'id']);
        }

在actionCreate中,您创建了BuktiPenerimaanInvoice和BuktiPenerimaanDebitNote的实例。然后加载发布数据并执行验证。一切都很好。

$modelsBuktiPenerimaanInvoice = new BuktiPenerimaanInvoice(); // to many record
    $modelsBuktiPenerimaanDebitNote = new BuktiPenerimaanDebitNote(); // to many record

但是,在actionUpdate中,您使用了以下代码,它返回一对多关系,这些关系是模型集合(模型数组):

$modelsBuktiPenerimaanInvoice = $model->buktiPenerimaanInvoices;
    $modelsBuktiPenerimaanDebitNote = $model->buktiPenerimaanDebitNotes;

您正在使用以下代码执行验证:

$modelsBuktiPenerimaanDebitNote->load($request->post())
            && $modelsBuktiPenerimaanInvoice->load($request->post())
            && Model::validateMultiple([$model, $modelsBuktiPenerimaanDebitNote, $modelsBuktiPenerimaanInvoice])) {

“validateMultiple”调用每个指定模型的“validate”方法。其中两个模型($ modelsBuktiPenerimaanDebitNote,$ modelsBuktiPenerimaanInvoice)是集合而不是对象。因此,在集合上调用“validate”会引发错误

"name":"PHP Fatal Error","message":"Call to a member function isAttributeRequired() on array","code":1,"type":"yii\\base\\ErrorException"