[1]
As you said, easy-peasy:
class User extends AppModel {
var $hasMany = array(
'Paper' => array('foreignKey' => 'author_id')
);
}
[2]
It's possible, but not recommended, to do it as you suggest:
class Paper extends AppModel {
var $belongsTo = array(
'Author' => array('className' => 'User'),
'Reviewer1' => array('className' => 'User', 'foreignKey' =>
'reviewer1_id'),
'Reviewer2' => array('className' => 'User', 'foreignKey' =>
'reviewer2_id'),
'Reviewer3' => array('className' => 'User', 'foreignKey' =>
'reviewer3_id')
);
}
class User extends AppModel {
var $hasMany = array(
'Paper' => array('foreignKey' => 'author_id'),
'ReviewedPaper' => array(
'className' => 'Paper',
'foreignKey' => false,
'conditions' => 'User.id=ReviewedPaper.reviewer1_id'
. ' OR User.id=ReviewedPaper.reviewer2_id'
. ' OR User.id=ReviewedPaper.reviewer3_id');
);
}
A better solution would be to create a HABTM relation. You'd need an
extra table, 'paper_reviewers' with keys `paper_id` and `reviewer_id`.
You'd then do :
class Paper extends AppModel {
var $belongsTo = array(
'Author' => array('className' => 'User')
);
var $hasAndBelongsToMany = array(
'Reviewer' => array('className' => 'User')
);
}
class User extends AppModel {
var $hasMany = array(
'Paper' => array('foreignKey' => 'author_id')
);
var $hasAndBelongsToMany = array(
'ReviewedPaper' => array('className' => 'Paper')
);
}
You'd have to enforce the limit of 3 manually though.
On May 28, 2:03 pm, Robert <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have just started playing with Cake and had some fights with
> validation. Now I am at the point where I need to use hasMany on my
> simple (or maybe complex) database. I am working on a web application
> for articles submission and review. For now I have two models - User
> and Paper with the following relations:
>
> - Papers table has a foreign key to Users table for author - from what
> I read this is pretty simple to implement - adding hasMany to User
> model and belongsTo to Paper model should do the trick
>
> - Papers table has 3 foreign keys to Users table for reviewers - and
> this is where my question appears - can Cake handle that? should I add
> 3 separate hasOne entries to Paper model pointing to User model? if
> so, what should be added on User model side? or maybe there should be
> 4 hasMany entries on User model each pointing to Paper model with
> different foreign keys set?
>
> Thanks in advance for all suggestions.
> Robert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---