2012-02-17 53 views
0

我有這樣錯誤Yii中多模型的形式獲得第二模型值

+------------------+ 
|  Invoices  | 
+------------------+ 
| id    | 
| customer_id (Fk) | 
| description  | 
+------------------+ 

+------------------+ 
| Customers  | 
+------------------+ 
| id    | 
| firstname  | 
| lastname   | 
| description  | 
+------------------+ 

數據庫,以便按我的需求量的我做了multimodel。在這多模式的客戶模型在發票model.Now的actionCreate(裝)發票控制器是這樣

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) 
     { 
     $customers->save(false); 
     $model->customer_id = $customers->getPrimaryKey(); 
     $model->save(false); 
     $this->redirect(array('view','id'=>$model->id)); 
     } 
    } 
    $this->render('create',array(
     'model'=>$model, 
     'customers' => $customers, 
    )); 
    } 

的的actionView()是尋找這樣

public function actionView($id) 
    { 
    $this->render('view',array(
     'model'=>$this->loadModel($id), 
     'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id, 
    )); 
    } 

現在,當我呈現的形式的RenderPartial和改變了我的代碼在瀏覽文件中像這樣

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model, 
    'attributes'=>array(
    'id', 
    'customer_id', 
    array(
     'label' => 'Firstname', 
     'value' => $customers->firstname, 
    ), 
    array(
     'label' => 'Lastname', 
     'value' => $customers->lastname, 
    ), 
    array(
     'label' => 'description', 
     'value' => $customers->description, 
    ), 
), 
)); ?> 

這表明這樣的錯誤試圖讓行非對象的特性「值」 => $客戶 - > firstname, 那麼有人可以告訴我哪裏錯了嗎?歡迎任何幫助和建議。

+0

改變你的驗證,是這樣的: ' $有效= $模型 - >的validate(); $ valid = $ customers-> validate()&& $ valid; ' – farzan 2012-02-20 05:03:27

+0

雅我已經改變它..它不起作用。 – NewUser 2012-02-20 05:36:44

+0

因此actionView在發票控制器中?這意味着'loadModel($ id)'正在加載發票? – 2012-02-21 13:51:52

回答

2

我認爲,在後續代碼的問題:

public function actionView($id) 
    { 
    $this->render('view',array(
     'model'=>$this->loadModel($id), 
     'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id, 
    )); 
    } 

試試這個代碼:

public function actionView($id) 
    { 
    $model = $this->loadModel($id); 
    $this->render('view',array(
     'model'=>$model, 
     'customers'=>Customers::model()->findByAttributes(array('id'=>$model->customer_id)), 
    )); 
    } 
+0

它顯示像未定義變量的錯誤:型號 – NewUser 2012-02-21 14:30:40

+0

在哪一行?你添加了$ model = $ this-> loadModel($ id);? – 2012-02-21 14:35:16

+0

嘿,謝謝Ramil Amr.Finalally它的工作.. – NewUser 2012-02-21 15:01:49

1

Invoices模型中客戶關係的名稱是什麼?我們假設它是customs。你可以試試這個:

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model, 
    'attributes'=>array(
    'id', 
    'customer_id', 
    array(
     'label' => 'Firstname', 
     'value' => $model->customs->firstname, 
    ), 
    array(
     'label' => 'Lastname', 
     'value' => $model->customs->lastname, 
    ), 
    array(
     'label' => 'description', 
     'value' => $model->customs->description, 
    ), 
), 
)); ?> 

不過,當然你有關係的實際名稱替換customs

+0

沒有。它不工作。錯誤是:試圖獲取非對象的屬性 – NewUser 2012-02-20 05:41:33

0

此錯誤表示您嘗試訪問非對象。大多數情況下,它不是從您渲染它的位置填充的。 可以肯定做以下

  1. echo CVarDumper($_GET['id'],100,true)
  2. echo CVarDumper($customers,100,true)

,並讓我們知道什麼是存在的。

所以actionView()應該是這樣的

public function actionView($id) 
    { 
    $this->render('view',array(
     'model'=>$this->loadModel($id), 
     `echo CVarDumper($_GET['id'],100,true)` 
     yii::app()->end(); 
     'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id, 
    )); 
    } 

展示何顯示。

第2步撤消上面的代碼到您的一部開拓創新的一個,然後添加

echo CVarDumper($customers,100,true) 
yii::app()->end(); 

<?php $this->widget('zii.widgets.CDetailView', array(..... 

,然後檢查哪些內容顯示

+0

這應該用於哪個文件?模型,視圖或控制器文件? – NewUser 2012-02-19 18:44:15

+0

1去你的地方,你正在使用'$ _GET'和2去查看文件 – 2012-02-19 19:35:34

+0

我沒有得到你。你可以詳細說明如何使用它? – NewUser 2012-02-20 05:36:12

0

變化

'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id']))->customer_id, 

'customers'=>Invoices::model()->findByAttributes(array('id'=>$_GET['id'])), 

如果findByAttributes返回null,您仍可能會得到相同的錯誤。所以你必須在$ customers-> firstname之前檢查$ customers是否爲null。

+0

它顯示錯誤,因爲Firstname屬性不是來自Invoice模型。錯誤如下:屬性「Invoices.firstname」未定義。 – NewUser 2012-02-21 14:10:22