Welcome Guest, Not a member yet? Register   Sign In
How to get data of joined table using query() method
#1

(This post was last modified: 02-22-2021, 12:23 AM by venance.)

Hello everyone.
I have an issue with getting data of the joined table if I use query().
I did not get the data of product table, how can I solve this using query() without using Active record?

Here is my db table structure

PHP Code:
category table
+--------------------------+
cId added  category  |       
+-----+--------+-----------+
|
  1  1.2.20 |  PC       |
|
  2  1.7.20 electron  |
+-----+--------+-----------+ 

PHP Code:
product table
+--------------------------+
id  |  cId   |  cost     |       
+-----+--------+-----------+
|
  1  |   1    |  3000     |
|
  1  |   2    |  9000     |
+-----+--------+-----------+ 

MY MODEL
PHP Code:
protected $table 'category';

public function 
showProduct(){
    
$sql "SELECT category.*, COALESCE(SUM(product.cost),0) as price 
            FROM category JOIN product ON product.cId = category.cId
            GROUP BY category.cId"
;
    
$this->db->query($sql);
    return 
$this;


MY CONTROLLER
PHP Code:
public function index()
{
    
$result $this->model->showProduct();
    echo 
"<pre>";
    
print_r($result->asObject()->paginate(1));    
        //pagination
    
$pager $this->model->showProduct()->pager->links();


Result I get
PHP Code:
Array(
    [
0] => stdClass Object
        
(
            [
cId] => 1
            
[added] => 1.2.20
            
[category] => PC
        
)
    [
1] => stdClass Object
        
(
            [
cId] => 2
            
[added] => 1.7.20
            
[category] => electron
        
),

Reply


Messages In This Thread
How to get data of joined table using query() method - by venance - 02-22-2021, 12:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB