2012-02-13 29 views
0

我有數據庫,就像這個外鍵在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不會改變。它每次都顯示零。有人能告訴我我錯在哪裏嗎?任何幫助和建議都會非常有價值。

回答

2

我的猜測是你用發票的外鍵覆蓋了客戶的主鍵。我並不是說那樣不正確(也許在你的場景中它是有道理的)。

讓我解釋一下你的代碼在做什麼:

  • 首先,創建兩個型號,發票和客戶的新實例。 Yii明白,因爲「他們希望在數據庫中插入新項目」。

  • 然後,檢查是否有來自ajax表單的項目。如果屬實,那麼,

  • 您填充發票(定義爲$model。如果需要進一步編輯和理解,我會將其更改爲$invoice)。
  • 您還會彈出客戶信息,覆蓋$valid的值(因此,您不知道發票實際上是否有效)。
  • 如果有效(記住你只是驗證客戶的信息),這樣做,
  • 保存發票
  • 覆蓋客戶的ID與發票的foreing關鍵客戶。
  • 保存客戶並重定向。現在

,我從有:

  • $valid預期不起作用:我會變成一個增量分配。
  • 您可能未通過來自ajax表格的customer_id。 Foreing鍵是整數,所以如果未在模型中定義,它將變爲0或NULL
  • 您總是將id = 0/NULL傳遞給Customer模型,所以在驗證時可能會提示您。但是,您正在使用save(false),這意味着它不會在保存時進行預先驗證,所以您永遠不會知道它不起作用。

因此,根據這一點:

public function actionCreate() 
    { 
    $invoice = new Invoices; 
    $customers = new Customers; 
    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($invoice); 

    if (isset($_POST['Invoices'],$_POST['Customers'])) 
    { 
     $invoice->attributes = $_POST['Invoices']; 
     $customers->attributes = $_POST['Customers']; 
     $valid = true; /* expect it is always valid */ 
     $valid &= $invoice->validate(); /* if $invoice is not valid, $valid will be false (true&false = false) */ 
     $valid &= $customers->validate(); /* same as the above line */ 
     if($valid) 
     { 
     $customers->save(); /* First save customers. It's the Foreign item */ 
     $invoice->customer_id = $customers->getPrimaryKey(); /* new instances use getPrimaryKey() to get its id */ 
     $invoice->save(); /* Save invoice AFTER getting customer's primary key */ 
     $this->redirect(array('view','id'=>$invoice->id)); 
     } 
    } 

    $this->render('create',array(
     'invoice'=>$invoice, 
     'customers'=>$customers, 
    )); 
    } 

我希望這能解決你的問題。

+0

韓感謝您的快速reply..but,這是行不通的 – NewUser 2012-02-13 18:13:53

+0

你能告訴我們任何錯誤?你甚至可以發佈來自$ invoice-> getErrors()和$ customers-> getErrors()(在validate()或save()之後)的錯誤。此外,我編輯了代碼,因爲我忘記在渲染方法中將'model'更改爲'invoice'。 – Korcholis 2012-02-13 18:18:36

+0

雅我已經做了必要的改變,你在這裏完成了..但它不工作。沒有顯示任何錯誤。只是外鍵不更新所有的數據都在輕鬆保存。 – NewUser 2012-02-13 18:30:36

0

請您在這裏需要了解清晰的scenerio。如果($ valid) {model-> save(false); $ customers-> id = $ model-> customer_id; $ customers-> save(false); (數組'('view','id'=> $ model-> id)); }

$ model-> save(false);告訴模型,如果這個記錄不是save(),它會設置$ customers-> id = $ model-> customer_id;

這隻會返回false,因爲。如果你在$ model-> save()之前調用($ customers-> id = $ model-> customer_id;),我寧願更喜歡。

請記住,如果需要檢查,如果保存()返回true,那麼將它設置爲$模型 - >另存(真)