Tuesday, August 6, 2013

legacy db with non standard foreignkey hasMany association


Hello,
I must work on a pretty old database and I am forced to use a table ( Campaign ) that didn't have a numeric primary key .
The former primary key was the field "code" varchar(6). So I had to add an autoincrement  id field.

Campaign hasMany Users

in the Campaign model I then have:

public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey'   => false,
'conditions' => 'User.campaign_id=Campaign.code'
                  )
);

in the User model I have:

public $belongsTo = array(
    'Campaign'=>array(
              'className' => 'Campaign',
              'foreignKey'=>false,
              'conditions'=> array('User.campaign_id = Campaign.code')
              ),
);


But when I go to /campaigns/view/103
the campaign data are shown correctly but if I want to show also the related Users belonging to the given Campaign_id 
I get 
Column not found: 1054 Unknown column 'Campaign.code' in 'where clause'

And the breaking query is:

SELECT `User`.`id``User`.`field2``User`.`field3``User`.`field4`  FROM `mydb`.`users` AS `User` WHERE `Campaign`.`code`=`User`.`campaign_id`

No join is created so th Campaign.code is unknown...


I know I should change the model structure but It would mean change a huge amount of things..


Please help
THANKS

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: