【发布时间】:2018-02-19 15:02:10
【问题描述】:
我正在尝试以数组的形式从我的数据库中提取客户订单。我的数据库结构有一个订单表,然后是一个连接两个表的订单 ID 字段的 order_items 表。
public function getUserOrders($id) {
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_items', 'order_items.order_id = orders.id', 'left');
$this->db->join('products', 'products.id = order_items.product_id', 'left');
$this->db->where('cust_id', $id);
$this->db->limit(5);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
我希望我的数组结构是嵌套的,这样在每个订单数组中都有一个用于该订单的嵌套产品数组,我可以在上面的模型中实现这一点吗?
【问题讨论】:
-
虽然 ORM 的答案是有效的,但如果您不需要定期重组数据集,那么该解决方案可能会有点矫枉过正。使用简单的循环重构结果并不难。如果您可以分享上述查询的示例结果集,则可能会有准确的答案。
标签: php arrays database codeigniter