我有數據庫,就像這個外鍵在MySQL中使用Yii
==== Invoices ====
id
costumer_id
description
==== Costumers ===
id
firstname
lastname
現在我已經取得了模型就像this.In發票模型之間的關係的關係是這樣的
public function relations()
{
return array(
'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id')
);
}
在負荷消費模式的關係就是這樣
public function relations()
{
return array(
'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
);
}
現在我的關係是指一個負荷消費有馬ny發票和發票屬於客戶。 現在我製作了multimodel,並將Costumer模型加載到Invoice模型中,就像這樣。
public function actionCreate()
{
$model = new Invoices;
$customers = new Customers;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Invoices'],$_POST['Customers']))
{
$model->attributes = $_POST['Invoices'];
$customers->attributes = $_POST['Customers'];
$valid = $model->validate();
$valid = $customers->validate();
if($valid)
{
$model->save(false);
$customers->id = $model->customer_id;
$customers->save(false);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'customers'=>$customers,
));
}
這裏的每件事都可以。我可以輕鬆地爲兩種模型插入數據。但是我的問題來自於當我從Invoice multimodel插入數據時,外鍵id不會改變。它每次都顯示零。有人能告訴我我錯在哪裏嗎?任何幫助和建議都會非常有價值。
韓感謝您的快速reply..but,這是行不通的 – NewUser 2012-02-13 18:13:53
你能告訴我們任何錯誤?你甚至可以發佈來自$ invoice-> getErrors()和$ customers-> getErrors()(在validate()或save()之後)的錯誤。此外,我編輯了代碼,因爲我忘記在渲染方法中將'model'更改爲'invoice'。 – Korcholis 2012-02-13 18:18:36
雅我已經做了必要的改變,你在這裏完成了..但它不工作。沒有顯示任何錯誤。只是外鍵不更新所有的數據都在輕鬆保存。 – NewUser 2012-02-13 18:30:36