j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Using php if else condition during query construction #337

Closed sanmayad closed 6 years ago

sanmayad commented 6 years ago

I want to do something like

    $expenses = ORM::for_table('account')
            ->select('task')
                     if($goal>0){
                      ->where('goal', $goal)
                     }
            ->find_many();

Can I do it in any way?

tag commented 6 years ago

Hi! The best place for help requests is StackOverflow. More people look there for questions to answer. Just tag the question with Idiorm, and you're likely to see very quick responses. GitHub is a great place to report bugs and suggest patches.

Without testing any code, the following seems like it should work ...

$expenses = ORM::for_table('account')->select('task');

if ($goal > 0) {
    $expenses = $expenses->where('goal', $goal)
}

$expenses = $expenses->find_many();