codeigniter-base-model icon indicating copy to clipboard operation
codeigniter-base-model copied to clipboard

Allow for where array in get_by() & get_many_by()

Open mikedfunk opened this issue 11 years ago • 6 comments

instead of allowing only one where filter, this allows passing a where array as the first param to get_by() and get_many_by().

mikedfunk avatar Oct 02 '12 14:10 mikedfunk

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.

ivantcholakov avatar Nov 08 '12 06:11 ivantcholakov

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.

ivantcholakov avatar Nov 08 '12 07:11 ivantcholakov

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);

ivantcholakov avatar Nov 09 '12 08:11 ivantcholakov

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.

jamierumbelow avatar Nov 09 '12 09:11 jamierumbelow

I made a pull request about the thing I mentioned a couple of posts earlier.

zbrox avatar Nov 14 '12 20:11 zbrox

0e11dbb works well with where array.

twnaing avatar Dec 14 '12 22:12 twnaing