Hey everybody
I have a User and a UserProfile model:
class UserProfile extends AppModel {
var $name = 'UserProfile';
var $belongsTo = array(
'User'
);
}
class User extends AppModel {
var $name = 'User';
var $hasOne = array(
'UserProfile'
);
}
Now I have a behavior with the following method:
function belongsToExistingRecord(&$model, $associationName,
$associatedId, $conditions = array()) {
$conditions = array_merge(array(
$associationName.'.id' => $associatedId
), $conditions);
if($model->{$associationName}->find(
'count',
array(
'conditions' => $conditions,
)
) > 0) return true;
}
This should check whether e.g. whether there exists a UserProfile for
User with ID 123:
// In controller
$this->UserProfile->belongsToExistingRecord('User', 123);
Which results in the following SQL statement:
SELECT *
FROM `users` AS `User`
LEFT JOIN `user_profiles` AS `UserProfile` ON
( `UserProfile`.`user_id` = `User`.`id` )
WHERE `User`.`id` = '123'
But because this is a left join, it always returns 1 when at least
exists a User with the given ID regardless of the existence of a
UserProfile. So I need to use an INNER join, but I don't know where to
tell CakePHP to use INNER? I tried in the association, but didn't seem
to work:
var $belongsTo = array(
'User' => array(
'type' => 'inner'
)
);
Anybody has an idea what I'm doing wrong?
Thanks,
Josh
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
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