⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to join 2 tables and render it



jayson

jayson
  • profile picture
  • Member

Posted 20 February 2018 - 15:38 PM

Hi all.

 

I am new for grocery crud.

 

I trying to join 2 table and display it. I wrote a custom model to extends crud model.

My custom model:

<?php
class Test_crud_model extends Grocery_crud_model {
    
    protected $primary_key = 'film_id';
    protected $table_name = 'film';
    
    function __construct()
    {
        parent::__construct();
    }
    
    function get($id = null, $order_by = null){
        if(is_numeric($id)){
            $this->db->where($this->primary_key,$id);
        }
        
        if(is_array($id)){
            foreach($id as $key => $_value){
                $this->db->where($key,$_value);
            }
        }
        
        $q =$this->db->get($this->table_name);
        return $q->result_array();
    }
}
?>

My controller:

function test_example(){
        $crud = new grocery_CRUD();
        
        $crud->set_model('Test_crud_model');
        $crud->set_table('film');
        $crud->result = $this->Test_crud_model->joinTable(array(
            'film_actor.film_id'=> 23
        ));
        
        $output = $crud->render();
        $this->testView($output);
    }

My output are display all data from "film" table.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 20 February 2018 - 16:59 PM

Well instead of performing the jointable in controller - why cant u perform all inside the model?? Just do it all inside the model and set the output...!! It should work.


jayson

jayson
  • profile picture
  • Member

Posted 21 February 2018 - 01:20 AM

Hi Amit Shah,

this is my joinTable function in model:

function joinTable($id = null, $order_by = null){
        if(is_numeric($id)){
            $this->db->where($this->primary_key,$id);
        }
        
        if(is_array($id)){
            foreach($id as $key => $_value){
                $this->db->where($key,$_value);
            }
        }
        $this->db->select('film.title, film_actor.actor_id')
                 ->join('film_actor', 'film.film_id = film_actor.actor_id');
        $result = $this->db->get($this->table_name);
        return $result->result_array();
    }

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 21 February 2018 - 17:39 PM

Well .. when u do a join table function call.. it dose make the join - and return u the data - the criteria for the db is set out - it will not not perform the join again - cuz its already executed the mechanism ..

now when u do a render - it will hit the get function on list method (ajax call) .. and there it is not performing the join - it is setting the data only from the 1st table.. so that is the reason why i was asking u to put all those in the get method itself .. 

 

i still didnt understand your purpose to actually on the 1st place go and split the methods and give an exclusive call from the controller - when u were actually could have all in 1 place and get the desired result..

 

Nothing personal on yr descission - it was just that i was a bit curious to understand the same.. thats why.. May be i will learn something new from you.