[英]Inner join in datamapper codeigniter
I have two table products
and categories
, I want to get all the products with their category name. 我有两个表
products
和categories
,我想获取所有产品的类别名称。
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 * FROM products p INNER JOIN categories c ON (p.category_id = c.id) WHERE p.user_id = 1
必需查询:
SELECT * FROM products p INNER JOIN categories c ON (p.category_id = c.id) WHERE p.user_id = 1
Model product.php 模型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 模型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 for CodeIgniter does not seem to have the ability to do INNER JOIN, and defaults to LEFT OUTER JOIN. 用于CodeIgniter的Datamapper ORM似乎没有执行INNER JOIN的功能,默认为LEFT OUTER JOIN。
Therefore I would use ActiveRecord for your INNER JOIN query. 因此,我将对您的INNER JOIN查询使用ActiveRecord。
$result_set = $this->db->join('categories', 'categories.id = products.category_id')->where('user_id = 1')->get('products')->result_array();
Source: I use DataMapper ORM in my job. 资料来源:我在工作中使用DataMapper ORM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.