连接表的最有效方法

发布于 2024-11-15 20:38:36 字数 772 浏览 3 评论 0原文

我在cakePHP中有两个模型,一个是Parts,另一个是Orders

Parts 是一个简单的模型,带有 id 列以及多个字段,例如 客户零件编号描述等。每个零件都有自己唯一的 id 。

Orders 是另一个简单模型,有一个名为 *part_id* 的字段,它表示 Parts 模型中的 id。

在一个操作中,在 Orders 控制器中,我需要提取该特定视图的所有订单,从 Parts 数据库中查找有关该订单的信息,并将其放在一起数组已准备好传递给视图。

例如,表格可能如下所示:

ORDERS

Part_id   id
2         5
1         7

PARTS

customer  partnumber  description    id
Google    XHA-V1      widget_1       1
Yahoo     YVS-XN      widget_5       2

由于我在任何一个视图中都会有 50-100 个订单,因此我需要一种有效的方法来引用 Parts 数据库中每个订单的 *part_id* 并将其合并到一个数组中。它多次搜索零件数据库,我不确定如何有效地实现。

注意:我想解决方案可能是蛋糕或纯 PHP。

I have two models in cakePHP, one is Parts and the other is Orders.

Parts is a simple model with an id column plusaction multiple fields such as customer, part number, description etc. Each part has its own unique id.

Orders is another simple model with a field called *part_id* which represents an id from the Parts model.

In an action, in the Orders controller I wll need to pull all the orders for that particualr view, find information about that order from the Parts database and put it together in an array ready to be passed to the view.

For example that tables might look like:

ORDERS

Part_id   id
2         5
1         7

PARTS

customer  partnumber  description    id
Google    XHA-V1      widget_1       1
Yahoo     YVS-XN      widget_5       2

Since I will have 50-100 orders in any one view, I need an efficient way to reference each order's *part_id* in the Parts database and combine this into one array. Its the searching of the Parts database multiple times that I'm unsure how to achieve effectively.

Note: I guess the solution could be cake or pure PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-11-22 20:38:36

如果您在订单模型中正确定义了关系,您应该能够检索所有相应的部件

$this->Orders->find('all');

cakephp 将在两个查询中执行此操作:
- 所有订单的选择
- 对所有产品进行选择,其中product.id在(list_of_Orders_ids)中

,然后它将以正确的格式构造一个数组

如果您需要特殊查询,您也可以“手动”设置查询的联接,但您将需要将递归设置为 -1

$this->Orders->find('all',array('recursive'=>-1,
                                'fields'=>array('Order.*','Part.*'),
                                'joins'=>array(array('table' => 'parts',
                                                     'alias' => 'Part',
                                                     'type' => 'INNER', //or left
                                                     'conditions' => array('Part.id = Order.part_id')))))

祝你好运!

If you have correctly defined the relation in your Order Model, you should be able to retreive all the corresponding Parts

$this->Orders->find('all');

cakephp will do it in two queries:
- a SELECT for all the Orders
- a SELECT for all the Products where product.id in ( list_of_Orders_ids )

and then it will contruct an array in a proper format

If you need an special query, you could also "manually" set the joins for the query, but you'll need to set the recursive to -1:

$this->Orders->find('all',array('recursive'=>-1,
                                'fields'=>array('Order.*','Part.*'),
                                'joins'=>array(array('table' => 'parts',
                                                     'alias' => 'Part',
                                                     'type' => 'INNER', //or left
                                                     'conditions' => array('Part.id = Order.part_id')))))

Good Luck!

时光与爱终年不遇 2024-11-22 20:38:36

您可以在控制器中使用 $this->Order->find('all', array('recursive' => 2)); 获取(所有)订单数组。每个订单还包含相关的零件信息。

我认为客户信息应该是订单的一部分,而不是部分。此外,您可能希望订单由多个部分组成。

You can get an array of (all) Orders using the $this->Order->find('all', array('recursive' => 2)); in your controller. Each order also contains the related Parts information.

Imo customer information should be part of the Order, not of Part. Also you might want Orders to consist of multiple Parts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文