Kohana 2.3.4 ORM数据透视表查询 [英] Kohana 2.3.4 ORM pivot table query

查看:127
本文介绍了Kohana 2.3.4 ORM数据透视表查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kohana的ORM查询数据透视表,并且想知道是否缺少内置函数.目前,我仅对表类别"和产品"设置2种模型.有一个数据透视表"categories_products",但是当用此插入数据时,我不需要模型:

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));

但是,我不知道如何在不为其创建模型的情况下进行查询. "join_table"函数仅返回数据透视表的名称(我认为首先选择了该表).如果您可以在没有模型的情况下将数据保存到数据透视表,那么在我看来,您应该能够以类似的方式检索数据.有什么想法吗?

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?

推荐答案

上面的内容实际上并不符合我的预期.我不断收到一个错误,指出该属性在模型中不存在.该脚本一直在工作.

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();

这篇关于Kohana 2.3.4 ORM数据透视表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆