使用Codeigniter中的QUERY BUILDER对JOINS的结果进行分组

时间:2017-03-30 06:14:21

标签: php mysql codeigniter builder

我有以下功能,参数是“asc,PV1”

public function get_products($order, $sku){
    if(is_array($result) && $result['active']){
        $id_emp = $result['id_emp'];
        $mts = 'module_tienda_status';
        $mtc = 'module_tienda_categories';
        $mtp = 'module_tienda_productos';
        $mtpt = 'module_tienda_product_type';
        $mtpa = 'module_tienda_product_attribute';
        $mta = 'module_tienda_attributes';
        $mtao = 'module_tienda_attribute_option';
        $mtpv = 'module_tienda_product_variation';
        $mtpvo = 'module_tienda_product_variation_options';
        $get = array(
            $mtp.'.id',
            $mtp.'.name',
            $mtp.'.sku',
            $mtp.'.desc_short',
            $mtp.'.desc_long',
            $mtp.'.id_category',
            $mtp.'.price',
            $mtp.'.id_status',
            $mtp.'.inventory',
            $mtp.'.imgs',
            $mtp.'.qty',
            $mtp.'.featured',
            $mtc.'.name as cat_name',
            $mtpt.'.id as type_id',
            $mtpa.'.id_attribute',
            $mtpa.'.id_option',
            $mta.'.name as attr_name',
            $mtao.'.option as opt_name',
            $mtpv.'.inventory as var_inv',
            $mtpv.'.qty as var_qty',
            $mtpv.'.price as var_price',
            $mtpvo.'.id_product_variation as var_id',
            $mtpvo.'.id_attribute as var_attr',
            $mtpvo.'.id_option as var_opt'
        );
        $this->db->select($get);
        $this->db->from($mts);
        $this->db->join($mtp, $mtp.'.id_status = '.$mts.'.id');
        $this->db->join($mtc, $mtp.'.id_category = '.$mtc.'.id', 'left');
        $this->db->join($mtpt, $mtp.'.id_product_type = '.$mtpt.'.id');
        $this->db->join($mtpa, $mtpa.'.id_product = '.$mtp.'.id', 'left');
        $this->db->join($mta, $mta.'.id = '.$mtpa.'.id_attribute', 'left');
        $this->db->join($mtao, $mtao.'.id = '.$mtpa.'.id_option', 'left');
        $this->db->join($mtpv, $mtp.'.id = '.$mtpv.'.id_product', 'left');
        $this->db->join($mtpvo, $mtpv.'.id = '.$mtpvo.'.id_product_variation', 'left');
        $this->db->where($mtp.'.id_emp', $id_emp);
        if($sku != 'null'){
            $this->db->where($mtp.'.sku', $sku);
        }
        if(!is_null($order)){
            $this->db->order_by('module_tienda_productos.created', $order);
        }
        $query = $this->db->get()->result();
    }else{
        return $result;
    }
}

结果是一个包含49行相同产品的数组,区别在于有不同的属性和变体,这里是示例https://pastebin.com/3X4w6wEi

我想要的是1个带有属性数组的单个结果和1个这样的变体数组(是json,请解码)

[{'id':'343','name':'Producto variable 1','sku':'PV1','desc_short':'<p><br></p>','desc_long':'<p><br></p>','id_category':null,'price':null,'id_status':'1','inventory':'0','imgs':null,'qty':'0','featured':'0','cat_name':null,'type_id':'2','attributes':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs','113':'s','114':'n','116':'xl'}}],'variations':[{'var_id':'42','var_inv':'0','var_qty':'0','var_price':'0.00','opts':[{'id_attribute':'49','attr_name':'Colors','opts':{'107':'Amarillo','110':'Celeste','121':'Rojo','122':'Azul'}},{'id_attribute':'57','attr_name':'Size','opts':{'112':'xs'}}]}]}]

0 个答案:

没有答案