doukuipei9938 2010-04-09 18:52
浏览 74
已采纳

Kohana 2.3.4 ORM数据透视表查询

I'm trying to query a pivot table with Kohana's ORM and I'm wondering if there is a built in function I'm missing. Currently I only have 2 models setup for the tables "categories" and "products". There is a pivot table "categories_products", but I don't need a model for it when inserting data with this:

$product = ORM::factory('product');
$product->add(ORM::factory('category', $addCat));

However, I can't figure out how to query it without creating a model for it. The "join_table" function only returns the name of the pivot table (which I thought selected the table at first). If you can save data to the pivot table without a model, it seems to me that you should be able to retrieve data in a similar way. Any ideas?

  • 写回答

2条回答 默认 最新

  • dpp66953 2010-04-26 16:58
    关注

    The above actually didn't work as predicted for me. I kept getting an error that the property didn't exist in the model. This script has been working though.

                    $pivot = ORM::factory('category')->join_table('products'); //pivot table name between products and categories
                $productsTotal = ORM::factory('product')->join($pivot, $pivot . '.product_id', 'id')->where('category_id', $id)->find_all();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douliao2493 2010-04-10 10:17
    关注

    You can access your categories without any explicit request like this:

    $product_object = ORM::factory('product', $your_product_id);
    
    foreach ($product->categores as category):
        //access category ORM object...
    endforeach;
    

    Kohana will request the categories for your product when you first try to access them I think.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 PCDN如何使用宽带的电视业务通道拨号叠加带宽?
  • ¥15 遇到这种校园宽带网络应该怎么样解决?
  • ¥30 AXI VIP验证多余打印问题
  • ¥15 利用加权最小二乘法求某品牌手机价格指标,已按照总销量计算出权重,各类型号手机价格已知,如何求得价格指标?
  • ¥15 如何自制一个硬件钱包,有兴趣的朋友一起交流
  • ¥15 (关键词-聊天软件)
  • ¥15 求大家看看这个编程的编法没有思路啊
  • ¥20 WSL打开图形化程序子窗口无法点击
  • ¥15 Jupyter Notebook 数学公式不渲染
  • ¥20 ERR_CACHE_MISS 确认重新提交表单