【问题标题】:Codeigniter: Nested Array from DBCodeigniter:来自数据库的嵌套数组
【发布时间】: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


【解决方案1】:

CodeIgniter 无法从 DB 请求中轻松实现开箱即用的嵌套数组。我会考虑使用ORMA quick search 提出了一些建议,但很有帮助的 this Stack Overflow answer 可能是最好的浏览方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多