0

I am having trouble creating relations on non-key fields. My issue is that all records from the 'HAS_MANY' table are being returned, rather than just the matching values specified in the 'on' section of the relation.

Table: customer
- id (PK)
- name
- reference_key

Table: visit
- id
- system_id
- reference_key

Model: Customer relations...
    'visits'=>array(self::HAS_MANY, 'Visit', '', 'on'=>'reference_key=visits.reference_key'),

Model: Visit relations...
    'customer'=>array(self::BELONGS_TO, 'Customer', 'reference_key'),

$dataSet = $data->visits(); // this all records from visit table instead of visits matching on the reference key. 
foreach($dataSet as $visit){
    echo 'visit key: '.$visit->reference_key.'; ';
}

Using 'alias' resolves the ambiguous column names issue. However, I'm getting non-matching results.

Any help is appreciated. Thanks.

Charles Brown
  • 25
  • 1
  • 3

2 Answers2

1

Try

'visits'=>array(self::HAS_MANY, 'Visit', array('reference_key'=>'reference_key')),
Keilo
  • 963
  • 1
  • 7
  • 13
  • Thanks you for the suggestion but it didn't work either. I've decided to not use the relations function and instead just access the ActiveRecord object: return Visit::model()->findAllByAttributes( array('visitor_key'=>$this->visitor_key) ); – Charles Brown Dec 01 '13 at 01:46
0

Having been unable to get the relations to work I've opted to simply add a method to retrieve the correct results

return Visit::model()->findAllByAttributes( array('visitor_key'=>$this->visitor_key) );

This takes care of it. Thank you!

Charles Brown
  • 25
  • 1
  • 3