2011-12-14 70 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' => '...'), 
     ), 
    }