联接不适用于模型

时间:2015-06-22 07:00:33

标签: codeigniter codeigniter-2

        //model
public function getAllPurchaseItems($purchase_id)
{
    $this->db->select('purchase_items.purchase_id,
    purchase_items.transfer_id, purchase_items.product_id,
    purchase_items.product_code, purchase_items.product_name,
    purchase_items.option_id, purchase_items.net_unit_cost,
    purchase_items.quantity, purchase_items.warehouse_id,
    purchase_items.item_tax, purchase_items.tax, purchase_items.discount,
    purchase_items.item_discount, purchase_items.expiry,
    purchase_items.subtotal, purchase_items.quantity_balance,
    purchase_items.date, purchase_items.status, categories.name');
    $this->db->from('purchase_items');
    $this->db->join(products, 'products.id = purchase_items.product_id');
    $this->db->join(categories, 'categories.id  = products.category_id');
    $this->db->order_by('purchase_items.id', 'asc');
    $q = $this->db->where(array('purchase_items.purchase_id' =>
        $purchase_id));

    if ($q->num_rows() > 0) {
        foreach (($q->result()) as $row) {
            $data[] = $row;
        }

        return $data;
    }
    return FALSE;
}

// controller
$this->data['rows'] = $this->purchases_model->getAllPurchaseItems($purchase_id);

1 个答案:

答案 0 :(得分:0)

你的模特就像这样

   $this->db->select('purchase_items.purchase_id, purchase_items.transfer_id, purchase_items.product_id, purchase_items.product_code, purchase_items.product_name, purchase_items.option_id, purchase_items.net_unit_cost, purchase_items.quantity, purchase_items.warehouse_id, purchase_items.item_tax, purchase_items.tax, purchase_items.discount, purchase_items.item_discount, purchase_items.expiry, purchase_items.subtotal, purchase_items.quantity_balance, purchase_items.date, purchase_items.status, categories.name');
    $this->db->from('purchase_items');
    $this->db->join('products', 'products.id = purchase_items.product_id');
    $this->db->join('categories', 'categories.id = products.category_id');
    $this->db->where(array('purchase_items.purchase_id' => $purchase_id));
    $q = $this->db->order_by('purchase_items.id', 'asc');
     $query = $this->db->get(); 
        if($query->num_rows() != 0)
        {
            return $query->result_array();
        }
        else
        {
            return false;
        }