Welcome Guest, Not a member yet? Register   Sign In
Address method in CI::DB via MY_Model
#1

[eluser]PhilTem[/eluser]
(It's a bit tricky finding a matching title)

I just ran into the following situation:
Writing my own MY_Model class to have CRUD-operations for every model inheriting MY_Model I'd love to have as many functions from the basic database-operations included into MY_Model.

To be more precise:
In addition to having functions like create, delete, update, count, count_all or others generic CRUD-operations I'd like to provide methods like truncate, empty_table, where, or_where and some other functions that are available via the active-record class by CI.
In my eyes it's cleaner to always only address one class from your controller i.e. to make something like this
Code:
$users = $this->user_m->limit(5, 5)->order_by('age', 'ASC')->get();
rather than
Code:
$this->db->limit(5, 5)->order_by('age', 'ASC');
$users = $this->user_m->get();

So I thought of overriding PHP's magic __call method. But in which way? First, I need to check for methods existing within my model's class and then check whether the function exists inside the active-record class.
But I don't see how to address the active-record class correctly.

Has anybody dealt with this problem? Or might have a hint or solution?
Or should I forget about that thing and just KISS?

Cheers,

Phil




Theme © iAndrew 2016 - Forum software by © MyBB