现在,使用gii,我创建了这样的模型:
表BuktiPenerimaan
class BuktiPenerimaan extends \yii\db\ActiveRecord{
/**
* @return \yii\db\ActiveQuery
*/
public function getInvoiceReports()
{
return $this->hasMany(InvoiceReport::className(), ['bukti_penerimaan_id' => 'id']);
}
}
和InvoiceReports:
class InvoiceReport extends \yii\db\ActiveRecord{
/**
* @return \yii\db\ActiveQuery
*/
public function getInvoiceReportDetails()
{
return $this->hasMany(InvoiceReportDetail::className(), ['invoice_id' => 'id']);
}
我的问题是,如果我创建了一个来自BuktiPenerimaan的对象,如何访问表invoice_report_detail
中的所有记录。
我这样用:
$model = $this->findModel($id); // model Bukti Penerimaan.
$dataInvoice = $model->invoiceReports; //exist
$dataInvoiceDetail = $model->invoiceReports->invoiceReportDetails // failed, always null.
请提示
答案 0 :(得分:0)
$dataInvoice = $model->invoiceReports;
是InvoiceReport
对象的数组。
您需要遍历每个InvoiceReport
以获得相关的InvoiceReportDetail
。
$model = $this->findModel($id); // model Bukti Penerimaan.
$dataInvoice = $model->invoiceReports; //exist , but array of objects
$dataInvoiceDetail = [];
foreach($dataInvoice as $dInvoice):
$dataInvoiceDetail[] = array_merge($dataInvoiceDetail,$dInvoice->invoiceReportDetails );
endforeach;
// $dataInvoiceDetail contains all invoice_report_detail