codeigniter-base-model
codeigniter-base-model copied to clipboard
Allow for where array in get_by() & get_many_by()
instead of allowing only one where filter, this allows passing a where array as the first param to get_by()
and get_many_by()
.
I've reached another solution: https://github.com/ivantcholakov/codeigniter-base-model/commit/4818bfe2c65fd43d16b75db340ca593631339258
It gives a possibility for injecting complex WHERE clauses. I hesitate whether it is beautiful enough.
The initial idea was something like this:
$this->load->model('products');
$search_list = $this->products->get_many_by(
$this->products->db
->where('out_of_stock', 0)
->group_start()
->like('name', 'sandals')
->or_like('description', 'sandals')
->group_end()
);
var_dump($search_list);
But I abandoned it.
The best way is by wrapping more DB methods, which I did having in mind that in CodeIgniter 3.0.0 some parameters (mainly $escape) have been added and some parameter default values have been changed. I tried to keep backward compatibility.The example becomes like this:
$this->load->model('products');
$search_list = $this->products
->where('out_of_stock', 0)
->group_start() // Works on CI 3.0.0
->like('name', 'sandals')
->or_like('description', 'sandals')
->group_end() // Works on CI 3.0.0
->get_many_by()
;
var_dump($search_list);
Just to let you guys know I am following this conversation, I'll spend a bit of time getting things discussed and merged this weekend. Thanks for your contribution and patience.
I made a pull request about the thing I mentioned a couple of posts earlier.
0e11dbb
works well with where array.