Welcome Guest, Not a member yet? Register   Sign In
[solved] Use of two models and active record
#1

[eluser]kaejiavo[/eluser]
Hi guys,

i just ran into a problem using active records.

In MY_Model i have some CRUD functions e.g.
Code:
function get_all($order_by = NULL, $limit = NULL, $offset = 0) {
if ($order_by !== NULL)
    $this->db->order_by($order_by);

if ($limit !== NULL)
    $this->db->limit($limit, $offset);

$this->db->from($this->table);
$query = $this->db->get();
return $query->result();
}

Model looks like e.g.:
Code:
class Job_detail extends MY_Model {

    function  __construct() {
        parent::__construct();

        $this->table = 'job_details';

        $this->db->select('job_details.*, jobs.name as job_name');
        $this->db->join('jobs', 'job_details.jobs_id = jobs.id');
    }
}
Code:
class Job extends MY_Model {
function  __construct() {
   parent::__construct();
    $this->table = 'jobs';
}
}

I can then use the crud methods of MY_Model with the joined tables.

Now my Problem comes up, when i have to use the two models in one controller, as $this->db->select() and $this->db->join() and their likes seem to work global.
So active record uses the select and join for every model i load from one controller and of course the join does not work properly from table 'jobs' in the Job model.

Could be i am not using this stuff as intended, can anybody help me out with some clues how to do this better or correct?

tia
Marco
#2

[eluser]mddd[/eluser]
$this->db->select and $this->db->join build parts of a query in the Database library. As there (usually) is only one Database library active, any $this->db calls willl influence it. So if in one model you say $this->db->join and then in another model you say $this->db->get, the join will still be active to the get call!

What you should do: make sure you don't issue commands to $this->db until you are really going to execute a query.
I understand you want a central place to set database parameters like you do now in the constructors. I suggest moving those commands out of the constructor into a new method. Maybe _set_db_params or something like that. Then before calling a get() kind of statement, you would call that method and the correct stuff is called.

Code:
// MY_Model

function get_all($order_by = NULL, $limit = NULL, $offset = 0) {

// this function is calling a get() so we can safely set the db params
$this->_set_db_params();

if ($order_by !== NULL)
    $this->db->order_by($order_by);

if ($limit !== NULL)
    $this->db->limit($limit, $offset);

$this->db->from($this->table);
$query = $this->db->get();
return $query->result();
}

// in the model
class Job_detail extends MY_Model {

    function  __construct() {
        parent::__construct();

        $this->table = 'job_details';
    }

    function _set_db_params() {
        $this->db->select('job_details.*, jobs.name as job_name');
        $this->db->join('jobs', 'job_details.jobs_id = jobs.id');
    }
}
#3

[eluser]kaejiavo[/eluser]
Thanks mddd,

your answer solved exactly my problem. It works like a charm, now!
I will drink a beer on you.

Cheers,
Marco




Theme © iAndrew 2016 - Forum software by © MyBB