CakePHP 不会为 hasOne 关系提取超过特定数量的行
我有一个客户,其数据库有几个相当大的相互关系模型。我们在我们正在生成的 $hasOne 关系中遇到了一个奇怪的“错误”。问题是这样的:
模型 UsersItems 与 Users 具有 $hasOne 关系。如下所示。
var $hasOne = array(
'Author' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
这将拉取并连接这些项目的别名“作者”的用户数据(在用户选择它们进行“书签”的上下文中,这些数据也属于“用户”)。
如果 UserItem 的 id
为 3445(表当前大小范围的中间位置),那么它就可以很好地通过。
但是,如果 UserItem 的 id
为 5000(表当前大小的上限),则 $hasOne 无法从 find()
中提取任何信息>。因此,较新的“加书签”的项目似乎不会检索作者数据,而其他(“较旧的”)项目则做得很好。
什么会导致这种情况?
添加信息
查询如下:
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
此查询确实工作:
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
请注意此特定位的差异:
LEFT JOIN `users` AS `Author` ON (`Author`.`id` = `Item`.`user_id`)
我拉错了< code>id 之前是作者 id,所以我知道这就是问题所在;现在我试图弄清楚如何让 Cake 生成这个正确的查询......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许您应该将项目模型中的作者关系切换为belongsTo而不是hasOne,如下所示:
book< /a> 解释得很好,belongsTo关系中的外键就是你所需要的:
希望我有帮助:)。
Maybe you should switch the Author relation to belongsTo instead of hasOne in your item Model like this :
the book explain it very well , foreign key in belongsTo relation is what you need :
Hope I helped :) .
如果我正确理解您的查询,您的关联应该是这样的:
在 UserItem 模型上:
在 Item 模型上:
我希望这会有所帮助。
If I understood your query correctly, your association should have been like this:
On UserItem model:
On Item model:
I hope this helps.
这里的答案都不适合我。无论好坏,我最终所做的是创建一个虚拟字段来填充作者姓名的值。
关系模型的问题是它们仅在您通过相关模型进行调用时才适用。就我而言,UsersItems 表和相关项目的创作者姓名之间不存在(也不应该存在)任何关系。
虚拟表在模型外观的每个实例中被调用。这意味着,即使在 UsersItems->Items 关系中,Items也会填充其虚拟字段。
我最终在 item.php 模型文件中创建的内容看起来有点像:
我会等待其他人的回复,了解这是否是解决此问题的合适解决方案,但这是迄今为止唯一一个解决方案为我工作。由于 UsersItems 表中没有关联的
author_id
列,因此我不知道如何关联这两个表。None of the answers here worked out for me. For better or worse, what I ended up doing was creating a virtual field that would populate the value of Author's name.
The problem with the relationships model is they only apply when you're calling through the model in question. In my case, there isn't (and should not be) any relationship between the UsersItems table and the name of the person who Authored the item in question.
Virtual tables are called in every instance of that model's appearance. That means that, even in the UsersItems->Items relationship, Items will populate its virtual fields.
What I ended up creating in the item.php model file looked a little something like:
I'll wait to hear back from others as to whether or not this is a suitable solution to this problem, but it's the only one so far that has worked for me. Because there is no associated
author_id
column in the UsersItems table, I just don't see how it'd be possible to relate the two tables.我的大胆猜测是,在某些时候,Author 和 UserItem 的 id 不同步,即 Author id 3445 hasOne UserItem id 3445 但 Author id 5000 hasOne UserItem 5001 或其他什么,并且查询尝试获取错误表的 id。但如果没有看到生成的查询,就很难说。
My wild guess is that at some point the ids of Author and UserItem have gone out of sync i.e. Author id 3445 hasOne UserItem id 3445 but Author id 5000 hasOne UserItem 5001 or something, and the query tries to fetch the id of a wrong table. But without seeing the generated queries it's hard to say.