Kohana:如何在一个查询中加载所有多对多关系
我有一个看起来像这样的数据库关系:
booking -> person <-> option
-> : one-to-many
<-> : many-to-many
我现在需要列出预订中的所有人员及其所有选项。在 kohana 中使用 ORM,我可以像这样加载所有人:
$persons = ORM::factory('booking', $id)->persons->find_all();
然后我可以循环这些人并获取他们的所有选项,但这意味着每个人一个查询。有没有一种聪明的方法可以加载这个而不必这样做?我想要的最终结果是这样的:
booking
└ id
└ created
└ persons
└ 0
└ id
└ name
└ options
└ 0
└ id
└ price
└ n
└ id
└ price
└ n
└ id
└ name
└ options
└ 0
└ id
└ price
└ n
└ id
└ price
我可能必须为此编写一个自定义数据库查询,但即使如此,我也不太确定该怎么做。我的 SQL-fu 不是最好的 :p
I have a database relationship that looks something like this:
booking -> person <-> option
-> : one-to-many
<-> : many-to-many
I now need to list all the persons in a booking, with all their options. Using ORM in kohana I can load all persons like this:
$persons = ORM::factory('booking', $id)->persons->find_all();
I could then loop over the persons and get all their options, but that would mean one query per person. Is there a clever way to load this without having to do that? What I would like to end up with is something like this:
booking
└ id
└ created
└ persons
└ 0
└ id
└ name
└ options
└ 0
└ id
└ price
└ n
└ id
└ price
└ n
└ id
└ name
└ options
└ 0
└ id
└ price
└ n
└ id
└ price
I probably have to write a custom database query for this, but even then I am not quite sure how to do it. My SQL-fu isn't the best :p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 ORM,则根本不需要编写任何 SQL。
您会发现本指南很有帮助。
http://www.scribd.com/doc/ 5022685/Kohana-PHP-ORM-指南-卷-1-
我认为这是针对 Kohana 版本 2 中的 ORM,但我不认为它有太大不同。您的里程可能会有所不同。
请注意,ORM 需要非常具体的命名约定。如果您的旧数据库的命名约定与 ORM 无法处理的不同,您将不得不放弃 ORM 并重新使用 SQL。 Kohana 帮助中有很多相关示例。
If you're using ORM you do not need to write any SQL at all.
You'll find this guide helpful.
http://www.scribd.com/doc/5022685/Kohana-PHP-ORM-Guide-Volume-1-
I think this is for ORM that was in version 2 of Kohana, but I don't believe it's terribly different. Your mileage may vary.
Be aware that ORM requires quite specific naming conventions. If you've got an old database that has a different naming convention than ORM can handle you'll have to ditch ORM and resort back to using SQL. There are lots of examples in the Kohana help for that.
$persons = ORM::factory('booking', $id)->persons->with('booking')->find_all();
您可以添加 <使用
load_with
属性的 autoloadnig 的 code>booking 关系:类 Model_Person 扩展 ORM {
}
因此,当您加载 Person 对象时,它会自动加入通过一次查询即可使用相关的预订模型。
$persons = ORM::factory('booking', $id)->persons->with('booking')->find_all();
You can add
booking
relation for autoloadnig usingload_with
property:class Model_Person extends ORM {
}
So, when you load Person object it will automatically join with related Booking model with one query.
根据我的经验,我可以说,只有在需要时才应该使用 ORM;有时你最好使用普通的 SQL 查询(使用数据库模块)
现在,当你希望结果是一个对象时,你可以在使用 ->execute() 方法之前使用 ->as_object('Model_Name') 方法数据库查询。
希望这有帮助:)
Based on my experience, I can say that you should use ORM only when you need it; sometimes you better use plain SQL queries (Using Database Module)
Now, when you want the result to be an object, you can use ->as_object('Model_Name') method, right before using ->execute() method on a Database query.
Hope this helps :)