php - Inner join in datamapper codeigniter -
i have 2 table products
, categories
, want products category name.
product table(products)
id (auto_increment) | name(varchar) | category_id(int) | price(decimal) | user_id(int)
category table(categories)
id(auto_increment) | name(varchar)
required query : select * products p inner join categories c on (p.category_id = c.id) p.user_id = 1
model product.php
class product extends datamapper { var $has_many = array('category'); var $validation = array( 'name' => array( 'label' => 'name', 'rules' => array('required', 'trim', 'alpha_dash', 'min_length' => 3), ), 'category_id' => array( 'label' => 'category id', 'rules' => array('required', 'trim', 'alpha_dash'), ), 'price' => array( 'label' => 'price', 'rules' => array('required', 'trim', 'alpha_dash'), ), 'quantity' => array( 'label' => 'quantity', 'rules' => array('required', 'trim', 'alpha_dash'), ), ); function get_product_from_userid($product_id){ $product_obj = new product($product_id); $product_obj->category->get(); } }
model category.php
class category extends datamapper { var $table = 'categories'; var $has_many = array('product'); }
controller(users)
class users extends ci_controller { function users(){ parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); } function index(){ $product_obj = new product(); $product_obj->user_id = 1; $product_id = 1; $product_obj->get_product_from_userid($product_id); } }
datamapper orm codeigniter not seem have ability inner join, , defaults left outer join.
therefore use activerecord inner join query.
$result_set = $this->db->join('categories', 'categories.id = products.category_id')->where('user_id = 1')->get('products')->result_array();
source: use datamapper orm in job.
Comments
Post a Comment