简体   繁体   中英

Codeigniter, Active record, join second table values as array

Two tables: first - kons, second - orders. Linked by field kons.id and order.id_kons
This is my query:

$this->db->select("k.id as k_id, DATE_FORMAT(k.datecreate, '%d/%m/%Y %H:%i:%s') as k_dt, o.id as o_id",false);
 $this->db->join('orders as o', 'o.id_kons = k.id','inner');
 $this->db->from('kons as k');<br>
 return $this->db->get()->result_array();

Result is (example, cut):

array(20) {
  [0]=>
  array(3) {
    ["k_id"]=>
    string(2) "45"
    ["k_dt"]=>
    string(19) "22/02/2014 15:41:35"
    ["o_id"]=>
    string(3) "533"
  }

  } .....

But i need something like that:

array(x) {
  [0]=>
  array(3) {
    ["k_id"]=>
    string(2) "45"
    ["k_dt"]=>
    string(19) "22/02/2014 15:41:35"
    ["o_id"]=>
    array(3) "533,534,536" or string(x) "533,534,536"
  }

You can use GROUP_CONCAT(expr) to get comma separated order ids how ever it is not recommended because of character length restriction of default set to 1024 character but it can be increased

$this->db->select("k.id as k_id, 
DATE_FORMAT(k.datecreate, '%d/%m/%Y %H:%i:%s') as k_dt, 
GROUP_CONCAT(o.id) as o_id",false);
$this->db->join('orders as o', 'o.id_kons = k.id','inner');
$this->db->from('kons as k');
$this->db->group_by("k.id");
return $this->db->get()->result_array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM