FaaPz/PDO

Where clause help

mridah opened this issue · 4 comments

I want to build a query like

select * from tbl1 where col1 >= 12 and col2  = 40;

have tried:

$query = $this->$pdo->select(['*'])
               ->from('tbl1')
               ->where('col1 >=', 12);
               ->where('col2 =', 40);

I even tried using:

              ->where(new \Clause\Conditional('col1', '>=', 12))

but it's not working

The first code gives me the error

Uncaught TypeError: Argument 1 passed to FaaPz\PDO\AdvancedStatement::where() must be an instance of FaaPz\PDO\Clause\Conditional, string given, called in filename....

and second gives me the error:

Uncaught Error: Class 'Clause\Conditional' not found in filename

even when I've used use \FaapZ\PDO\Clause\Conditional;

Can you please help me out ? @kwhat @FaaPz

kwhat commented

Hi @mridah,

I think you are pretty close to what you want, you just need the grouping object to produce the and conditionals.

use FaapZ\PDO\Clause;
...
$query = $this->pdo
        ->select() // ['*'] is the default.
        ->from('tbl1')
        ->where(
                new Clause\Grouping( // Combine Conditionals by 'AND'
                        'AND',
                        new Clause\Conditional('col1', '>=', 12) // New Conditional should take 3 arguments.
                        new Clause\Conditional('col2', '=', 4)
                )
        );

Hopefully that should clarify the new API. Please let me us know if you are still experiencing difficulty.

Thanks for the fast reply @kwhat . It's working !!

I read one of the earlier issues which said that ->where('col1 >=', 12) syntax was working in version 2.0.0, when did this change? In 2.0.1?

kwhat commented

It shouldn't have. 2.0.1 should have just been switching away from exceptions due to issues in php 7.2 and __toString.

Okay, thanks for info 👍