I have a client with a database that has several sizeable and interrelational models. We've come across an odd "bug" in a $hasOne relationship we're producing. The problem is this:
The model UsersItems has a $hasOne relationship with Users. It is as follows
var $hasOne = array(
'Author' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
This pulls and joins user data with the alias "Author" for these items (which also belong to Users in the context of the users having selected them for "bookmarking").
If the UserItem has an id
of, say, 3445 (more middle of the range of the current size of the table), it pulls through just fine.
However, if a UserItem has, say an id
of 5000 (the upper range of the table's current size), that $hasOne fails to pull in any information from a find()
. Consequently, it would seem newer "bookmarked" items do not retrieve Author data, while others ("older") do just fine.
What would be causing this?
ADDED INFORMATION
The query is as follows:
SELECT `UsersItem`.`id`, `UsersItem`.`user_id`, `UsersItem`.`item_id`,
`UsersItem`.`in_list`, `User`.`id`, `User`.`username`, `User`.`password`,
`User`.`email`, `User`.`group_id`, `User`.`resethash`, `User`.`confirmhash`,
`User`.`confirmed`, `Item`.`id`, `Item`.`user_id`, `Item`.`name`,
`Item`.`category_id`, `Item`.`description`, `Item`.`pagelink`,
`Item`.`purchaselink`, `Item`.`moderated`, `Item`.`image_filename`,
`Item`.`votecount`, `Item`.`parent_id`, `Item`.`discover_order`,
`Item`.`created`, `Item`.`slug`, `Item`.`normalized_name`,
((`Item`.`votecount`)/pow((3600*TIMEDIFF(`Item`.`created`, NOW()) + 12), .42)) AS `Item__rank`,
`Author`.`id`, `Author`.`username`, `Author`.`password`, `Author`.`email`,
`Author`.`group_id`, `Author`.`resethash`, `Author`.`confirmhash`,
`Author`.`confirmed`
FROM `users_items` AS `UsersItem`
LEFT JOIN `users` AS `User` ON (`UsersItem`.`user_id` = `User`.`id`)
LEFT JOIN `items` AS `Item` ON (`UsersItem`.`item_id` = `Item`.`id`)
LEFT JOIN `users` AS `Author` ON (`Author`.`id` = `UsersItem`.`id`)
WHERE `UsersItem`.`user_id` = 1118
AND `UsersItem`.`in_list` = 1
ORDER BY `UsersItem`.`id` DESC
SECOND ADDENDUM
This query does work:
SELECT `UsersItem`.`id`, `UsersItem`.`user_id`, `UsersItem`.`item_id`, `UsersItem`.`in_list`, `User`.`id`, `User`.`username`, `User`.`password`, `User`.`email`, `User`.`group_id`, `User`.`resethash`, `User`.`confirmhash`, `User`.`confirmed`, `Item`.`id`, `Item`.`user_id`, `Item`.`name`, `Item`.`category_id`, `Item`.`description`, `Item`.`pagelink`, `Item`.`purchaselink`, `Item`.`moderated`, `Item`.`image_filename`, `Item`.`yeekcount`, `Item`.`parent_id`, `Item`.`discover_order`, `Item`.`created`, `Item`.`slug`, `Item`.`normalized_name`, ((`Item`.`yeekcount`)/pow((3600*TIMEDIFF(`Item`.`created`, NOW()) + 12), .42)) AS `Item__rank`, `Author`.`id`, `Author`.`username`, `Author`.`password`, `Author`.`email`, `Author`.`group_id`, `Author`.`resethash`, `Author`.`confirmhash`, `Author`.`confirmed` FROM `users_items` AS `UsersItem` LEFT JOIN `users` AS `User` ON (`UsersItem`.`user_id` = `User`.`id`) LEFT JOIN `items` AS `Item` ON (`UsersItem`.`item_id` = `Item`.`id`) LEFT JOIN `users` AS `Author` ON (`Author`.`id` = `Item`.`user_id`) WHERE `UsersItem`.`user_id` = 1118 AND `UsersItem`.`in_list` = 1 ORDER BY `UsersItem`.`id` DESC
Note the difference in this particular bit:
LEFT JOIN `users` AS `Author` ON (`Author`.`id` = `Item`.`user_id`)
I was pulling the wrong id
as the Author id previously, so I know that was the problem; now I'm trying to figure out how to get Cake to generate this correct query...