Hi All,
I’ve been working at this for a couple days and can’t seem to find a way to get it to work.
I’ve found a other threads about a similar issue but haven’t seen a clear answer.
http://www.yiiframework.com/forum/index.php?/topic/11892-display-data-from-other-model-by-some-key/
http://www.yiiframework.com/forum/index.php?/topic/25542-relations-with-non-primary-keys/
http://www.yiiframework.com/forum/index.php?/topic/25229-help-in-relations/
http://www.yiiframework.com/forum/index.php?/topic/7125-multi-column-foreign-keys-and-ar-relations/
I have 3 mysql tables sales_rep, customer, and rep_view_customer. rep_view_customer is a mysql view and does not have a primary set(mysql does not have the functionality) but each row does have a unique id.
–sales_rep–
id
first_name
last_name
phone
address
city
state
zip
date_added
–commissions–
id
date_added
order_id
salesrep_id
customer_id
commission_total
status
–rep_view_customer–
entity_id
first_name
last_name
company
The relations of the models is: commissions is related to sales_rep(commissions.salesrep_id->sales_rep.id)
and rep_view_customer(commissions.customer_id->rep_view_customer.entity_id)
I’m trying to get some of the values to from rep_view_customer in CGridView.
Here’s my CGridView portion
<b style="font-size: 1.3em">Commissions</b>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'sales-rep-grid',
'dataProvider'=>$commissions->search(),
'filter'=>$commissions,
'columns'=>array(
'id',
/**/
//'rep_view.increment_id',
'rep_view_customer.first_name',
'customer_id',
'date_added',
'commission_total',
'status',
/**/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Here’s the actionView from my controller file
public function actionView($id)
{
$commissions = new Commissions;
$commissions->salesrep_id = $id;
$this->render('view',array(
'model'=>$this->loadModel($id),
'commissions'=>$commissions,
));
}
And here’s the relations method from my Commissions model
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id'),
'salesrep' => array(self::BELONGS_TO, 'SalesRep', 'salesrep_id'),
'rep_view_customer' => array(self::HAS_ONE, 'RepViewCustomer', '', 'on'=>'rep_view_customer.entity_id = t.customer_id'),
);
}
From what i read i though the ‘on’ option was the correct option to use. but when i try to view the view i get the error.
[i]CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘t.customer_id’ in ‘where clause’. The SQL statement executed was: SELECT rep_view_customer
.entity_id
AS t1_c0
, rep_view_customer
.email
AS t1_c1
, rep_view_customer
.first_name
AS t1_c2
, rep_view_customer
.last_name
AS t1_c3
, rep_view_customer
.company
AS t1_c4
FROM rep_view_customer
rep_view_customer
WHERE (rep_view_customer.entity_id = t.customer_id)[/i]
Any Thoughts on how i might get this figure out?
Thanks
Oliver