pixie icon indicating copy to clipboard operation
pixie copied to clipboard

pass array to where

Open jonasfrey opened this issue 7 years ago • 6 comments

instead of $query->where("post_id" , 1)->where("category_id" , 2);

i would like to

$query->where([
"post_id" => 1, 
"category_id" => 2
]);

Edit:

i extendet the code by myself: src/Pixie/QueryBuilder/QueryBuilderHandler.php between line 608 and 609 i inserted

        //if an array is given 
        if(is_array($key)){
            foreach($key as $key => $value){
                $cached = $this->where($key, $value);
            }
            return $cached;
        } 

jonasfrey avatar Jan 11 '19 14:01 jonasfrey

I believe your suggestion is not improving the code directly. The way of defining the where is something personal. I like to see the where in multiple lines because its more clear how many wheres I am using.

eL-Prova avatar Jan 13 '19 22:01 eL-Prova

yeah thats your opinion

jonasfrey avatar Jan 14 '19 16:01 jonasfrey

I'm with @eL-Prova on this.

TCB13 avatar Jan 14 '19 20:01 TCB13

I would like have the same thing.

ashickur-rahman avatar Dec 27 '20 10:12 ashickur-rahman

I think, current version is more read, then u propose. U unlikely transfer more then 2-3 parameters to query. Why use an array?

tooBot avatar Dec 29 '20 06:12 tooBot

Common use for this is checking if the record exists:

$insert = [
  'name' => 'John',
  'age' => 35,
  'sex' => 'Y',
  etc...
];

if (!QB::table('my_table')->where($insert)->first())
  QB::table('my_table')->insert($insert);

So this makes sense.

dragonattack avatar Jul 08 '21 00:07 dragonattack