ForeignKey false
To have an association which does not use the primary key of the related model in join conditions - the standard way to do that would be to use 'foreignKey' => false
.
I.e. whereas this association:
class Comment extends AppModel {
public $belongsTo = array(
'Profile' => array(
)
);
}
Will generate this sql:
SELECT ... LEFT JOIN profiles on ON (Profile.id = Comment.profile_id)
Specifying that a foreignKey isn't to be used like so:
class Comment extends AppModel {
public $belongsTo = array(
'Profile' => array(
'foreignKey' => false
)
);
}
Will produce this (invalid) sql:
SELECT ... LEFT JOIN profiles on ON ()
From this point, the desired conditions can be specified using the conditions array key:
class Comment extends AppModel {
public $belongsTo = array(
'Profile' => array(
'foreignKey' => false,
'conditions' => array(
'Comment.username = Profile.username'
),
)
);
}
(Note that the conditions are defined as a string) resulting in:
SELECT ... LEFT JOIN profiles on ON (Comment.username = Profile.username)