Kohana 3 ORM:高级查询、效率
所以我们都知道 Kohana 3 的文档绝对是糟糕的。那么,我如何构造以下查询,其中我有“玩家”和“龙”模型?
SELECT * FROM `dragons` JOIN `players` ON (`dragons`.`player_id` = `players`.`player_id`) WHERE `uid` IN (1,2,3) ORDER BY `dragons`.`id` ASC
我可以使用 ORM::factory('dragon')->join("players")->on("dragons.player_id", "=", "players.player_id") 来到达连接部分,但我无法执行 in
子句。 Kohana 3 的 ORM 中没有 in
函数。我尝试了 where
函数,但它在第三个参数周围加上了引号,因此我的 ID 列表变成了字符串,并且查询变得无效。那我能做什么呢?我不知道如何将 SQL 的自定义位添加到我的 ORM 加载查询中。同样,因为文档不存在。
我现在能想到的唯一方法就是加载所有合适的玩家,循环遍历他们,并获取他们的龙。但这看起来真的很愚蠢——查询次数比必要的多得多。我以前也有过这样的感觉,使用 ORM 会使查询端的效率变得非常低下,以便让编码变得稍微容易一些。这是真的吗?
So we all know that the documentation for Kohana 3 is absolutely horrible. So how can I construct the following query, where I have a "Player" and "Dragon" model?
SELECT * FROM `dragons` JOIN `players` ON (`dragons`.`player_id` = `players`.`player_id`) WHERE `uid` IN (1,2,3) ORDER BY `dragons`.`id` ASC
I can use ORM::factory('dragon')->join("players")->on("dragons.player_id", "=", "players.player_id")
to get to the join part, but I can't do the in
clause. There's no in
function in Kohana 3's ORM. I tried the where
function, but it puts quotes around the third parameter, so my list of IDs becomes a string and the query becomes invalid. So what can I do? I can't figure out how to add custom bits of SQL to my ORM loading queries. Again, because documentation doesn't exist.
The only way I can think of right now is to just load all the appropriate players, loop through them, and fetch their dragons. But this seems really stupid - way more queries than necessary. I've felt like this before, that using ORM makes things horribly inefficient on the querying end, in order to make it slightly easier to code. Is this true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ORM 可能会产生一些不太理想的查询 - 但它也会加快开发时间并为您提供抽象(有些人认为他们永远不必再手动编写 SQL)。
大多数时候,我更喜欢在复杂的 Web 应用程序中使用 SQL。
然而,话虽如此,使用 Kohana 3 的 ORM 可以实现您想要的效果。
来自像素开发人员。
这样做的优点是没有循环。 :-)
ORM can produce some less than desirable queries - but it also speeds development time and gives you an abstraction (some people feel they should never have to write SQL by hand anymore).
Most of the time I prefer SQL in complicated web applications.
However, in saying that, what you want is possible using Kohana 3's ORM.
From The Pixel Developer.
This has the advantage of not having the loop. :-)