With Policy hasMany Notification, using the find() on Policy creates a
LEFT JOIN with Notification. Since you want all notifications (with
user_id = $me) whether or not there is a Policy, the join should
either be Notification LEFT JOIN Policy or Policy RIGHT JOIN
Notification. (For more explanation of the different types of JOINS,
LEFT vs. RIGHT, see: http://www.w3schools.com/Sql/sql_join.asp, or
Google "SQL JOIN TYPES")
Rather than
$this->Policy->find('all', array(
'contain' => array(
'Notification',
... other models related to Policy ...
'conditions' => array(
'Notification.user_id' => $me,
'OR' => array(
'Policy.status' => 0,
'Policy.status IS NULL'
)
)
);
you can try
// Notification LEFT JOIN Policy
$this->Policy->Notification->find('all', array(
'contain' => array(
'Policy' => array(
... other models related to Policy (excluding Notification)...
)
),
'conditions' => array(
'Notification.user_id' => $me,
'OR' => array(
'Policy.status' => 0,
'Policy.status IS NULL'
)
)
);
The only other option that I can see is to ignore the hasMany
Notification relation (i.e. don't include it in the 'contain' option)
and manually define the RIGHT JOIN (see:http://book.cakephp.org/view/
1047/Joining-tables), setting 'type' => 'RIGHT'
On Feb 16, 9:31 pm, "Krissy Masters" <[email protected]>
wrote:
> Paginating the Policy with contains since the Policy is related to many
> models where the Notification is only related to Notification
>
> SQL spits:
> ...monster contain JOINS cut from code.....
>
> WHERE `Notification`.`user_id` = 'adffd636' AND ((`Policy`.`status` = 0) OR
> (`Policy`.`status` IS NULL)) LIMIT 15
>
> I get 3 records returned (Notifications with a Policy) Sorry when I say
> "with a Policy" I mean that there is an actual record of a Policy in the
> table.
> Yet I have 5 records in the db ,3 internal, 2 external so in the end I need
> 5 records
>
> K
>
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php