2011-12-14 99 views
2

我有一個mysql表和一個mysql視圖我試圖建立關係。Yii與非主鍵的關係

表(commissions)如下:

--commissions-- 
id(primary Key) 
date_added 
order_id 
salesrep_id 
customer_id 
commission_total 
status 

視圖(rep_view_customer)如下:

--rep_view_customer-- 
entity_id 
email 
first_name 
last_name 
company 

我想涉及到rep_view_customercommissionscommissions.customer_id = rep_view_customer.entity_id

我使用on選項嘗試:

'rep_view_customer' => array(self::HAS_ONE, 'RepViewCustomer', '', 'on'=>'rep_view_customer.entity_id = t.customer_id') 

我也嘗試使用設置主鍵爲rep_view_customer型號:

public function primaryKey(){ 
    return 'entity_id'; 
} 

但我似乎總是有一個錯誤落得類似於:

CDbCommand無法執行SQL語句:SQLSTATE [42S22]: 未找到列:1054'where clause'中的未知列't.customer_id'。執行的SQL語句是:SELECT rep_view_customerentity_id AS t1_c0rep_view_customeremail AS t1_c1rep_view_customerfirst_name AS t1_c2rep_view_customerlast_name AS t1_c3rep_view_customercompany AS t1_c4rep_view_customer rep_view_customer WHERE(rep_view_customer.entity_id = t.customer_id)

我在我束手無策嘗試下一步是什麼

+0

請你詳細說明控制器功能上的代碼,'調用'rep_view_customer?因爲它似乎從CdbCommand調試器沒有聯接發生 – ZaQ 2011-12-17 03:07:33

+0

@ZaQ`它似乎從CdbCommand調試器沒有聯接發生`我對此也感到困惑。我使用由Gii創建的默認Admin CGridView並添加了一個'rep_view_customer.first_name'列。我只收到錯誤,如果我添加相關的列。 – Mindphyre 2011-12-19 17:17:37

回答

0

你必須創建內部repview_customer的外鍵涉及佣金。你只用一個主鍵就無法做到這一點。

0

我是這樣做的,它的工作原理如下: 所以在你放置的佣金模型中:(它在模型中通過自身完成關係,然後連接輸出將與正常關係相同,但是與其他唯一鍵在佣金模式)

public function relations() 
    { 
     return array(
      'commission' => array(self::HAS_ONE, 'Commission', 'entity_id', 'on' => 'commission.customer_id=rep_view_customer.entity_id'), 
      'rep_view_customer' => array(self::HAS_MANY, 'RepViewCustomer', '', 'through' => 'commission', 'condition' => '...'), 
     ), 
    }