I have a relation on my database like this :
My goal is, when I am inserting a record into bukti_penerimaan table, I also save a lot of record into bukti_penerimaan_invoice
table and also a lot of record in bukti_penerimaan_debit_note
table.
Model
<?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']);
}
}
?>
So this is the actionCreate, which is successfully work.
Controller
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"])
];
}
}
}
view : create.php
<?= $this->render('_form', [
'model' => $model,
'modelsBuktiPenerimaanInvoice' => $modelsBuktiPenerimaanInvoice,
'modelsBuktiPenerimaanDebitNote' => $modelsBuktiPenerimaanDebitNote
]) ?>
view _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(); ?>
Everything is fine.
But when in actionUpdate, I got an error.
Controller
<?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"])
];
}
}
}
?>
The error is
"name":"PHP Fatal Error","message":"Call to a member function isAttributeRequired() on array","code":1,"type":"yii\\base\\ErrorException"
The problem is in :
<?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 ...']
]); ?>
How can to Yii2 recognized it as a form one to many relation.
Update
This is the actionFindModel looks like :
protected function findModel($id)
{
if (($model = BuktiPenerimaan::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Please an advise.
In "BuktiPenerimaan" model you have defined two one-to-many relations : "getBuktiPenerimaanDebitNotes" and "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']);
}
In actionCreate, you have created the instance of BuktiPenerimaanInvoice and BuktiPenerimaanDebitNote . Then you load the post data and perform validation. Everything good here.
$modelsBuktiPenerimaanInvoice = new BuktiPenerimaanInvoice(); // to many record
$modelsBuktiPenerimaanDebitNote = new BuktiPenerimaanDebitNote(); // to many record
But, in actionUpdate, you have used the below code and it returns you one to many relations which are collections of models (an array of models) :
$modelsBuktiPenerimaanInvoice = $model->buktiPenerimaanInvoices;
$modelsBuktiPenerimaanDebitNote = $model->buktiPenerimaanDebitNotes;
You are using the below code to perform validation :
$modelsBuktiPenerimaanDebitNote->load($request->post())
&& $modelsBuktiPenerimaanInvoice->load($request->post())
&& Model::validateMultiple([$model, $modelsBuktiPenerimaanDebitNote, $modelsBuktiPenerimaanInvoice])) {
"validateMultiple" calls "validate" method of each of the specified models . Two of those models ($modelsBuktiPenerimaanDebitNote, $modelsBuktiPenerimaanInvoice) are collections and not objects. So, calling "validate" on collection raising the error
"name":"PHP Fatal Error","message":"Call to a member function isAttributeRequired() on array","code":1,"type":"yii\\base\\ErrorException"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.