简体   繁体   English

Kohana:如何在一个查询中加载所有多对多关系

[英]Kohana: How to load all many-to-many relationships in one query

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: 在kohana中使用ORM,我可以像这样加载所有人:

$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 我的SQL-fu不是最好的:p

Based on my experience, I can say that you should use ORM only when you need it; 根据我的经验,我可以说您仅在需要时才应使用ORM。 sometimes you better use plain SQL queries (Using Database Module) 有时您最好使用普通的SQL查询(使用数据库模块)

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. 现在,当您希望结果成为对象时,可以在数据库查询中使用-> execute()方法之前,使用-> as_object('Model_Name')方法。

Hope this helps :) 希望这可以帮助 :)

If you're using ORM you do not need to write any SQL at all. 如果您使用的是ORM,则根本不需要编写任何SQL。

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. http://www.scribd.com/doc/5022685/Kohana-PHP-ORM-Guide-Volume-1-我认为这是适用于Kohana版本2中的ORM,但我认为这并没有太大不同。 Your mileage may vary. 你的旅费可能会改变。

Be aware that ORM requires quite specific naming conventions. 请注意,ORM需要非常特定的命名约定。 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. 如果您有一个旧的数据库,其命名约定与ORM不能处理的约定不同,则您必须放弃ORM并重新使用SQL。 There are lots of examples in the Kohana help for that. Kohana帮助中有很多示例。

  1. Try $persons = ORM::factory('booking', $id)->persons->with('booking')->find_all(); 试试$persons = ORM::factory('booking', $id)->persons->with('booking')->find_all();
  2. You can add booking relation for autoloadnig using load_with property: 您可以使用load_with属性为autoloadnig添加booking关系:

    class Model_Person extends ORM { Model_Person类扩展ORM {

     protected $_belongs_to = array('booking'); protected $_load_with = array('booking'); 

    } }

So, when you load Person object it will automatically join with related Booking model with one query. 因此,当您加载Person对象时,它将通过一个查询自动与相关的Booking模型联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM