开发者

Kohana 3 ORM: advanced queries, efficiency

开发者 https://www.devze.com 2023-01-25 06:05 出处:网络
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?

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('dr开发者_运维问答agon')->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?


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.

where('field', 'IN', range(1, 3));

This has the advantage of not having the loop. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消