我已经订购了#39;包含ID,日期等订单详细信息的表格以及表格' order_products'包含(order_id,product_id)
我想创建一个SELECT,它将返回包含所有产品id的内部数组的数组
示例:$ array =(id,date,...,array products(id1,id2 ...))
这是我的代码:
public function get_order_single($id)
{
$query = $this->db->select( '
orders.id,
orders.customer_id AS customerID,
orders.date,
orders.payment_method AS paymentMethod,
orders.total_price AS totalPrice,
orders.delivery_address AS address,
orders.modified,
orders.created')
->from( 'orders' )
->where('id',$id)
->get()
->result( 'array' );
return $query[0];
}
我需要添加什么才能从第二个表中获取所有信息?
答案 0 :(得分:1)
public function get_order_single($id)
{
// Get your order
$order = $this->db->select('
orders.id,
orders.customer_id AS customerID,
orders.date,
orders.payment_method AS paymentMethod,
orders.total_price AS totalPrice,
orders.delivery_address AS address,
orders.modified,
orders.created')
->from('orders')
->where('id', $id)
->get()
->row();
// If order is exists, lets find products for it
if ($order) {
// Here we do a join also to your pivot table
$order->products = $this->db
->where_in('order_products.order_id', $order->id)
->join('order_products', 'order_products.product_id = products.id', 'left')
->get('products')
->result();
}
return $order;
}
答案 1 :(得分:0)
尝试做这样的事情:
$this->db->select('name, value'); // SELECT which columns you want
$this->db->join('attribute', 'attribute.id = product_attribute.attribute_id', 'left'); // JOIN 1st table
$this->db->join('attribute_value', 'attribute_value.attribute_id = product_attribute.attribute_id', 'left'); // JOIN 2nd table
$this->db->where('product_id', $id); // CONDITION
$query = $this->db->get('product_attribute'); // FROM clause
return $query->result();