Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple OR'ed conditions support #201

Merged
merged 1 commit into from
May 28, 2014
Merged

Conversation

lrlopez
Copy link
Contributor

@lrlopez lrlopez commented May 18, 2014

Multiple OR'ed conditions

You can add simple OR'ed conditions to the same WHERE clause using where_any_is. You should specify multiple conditions using an array of items. Each item will be an associative array that contains
a multiple conditions.

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('name' => 'Joe', 'age' => 10),
                    array('name' => 'Fred', 'age' => 20)))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR
                                  ( `name` = 'Fred' AND `age` = '20' ));

By default, it uses the equal operator for every column, but it can be overriden for any column using a second parameter:

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('name' => 'Joe', 'age' => 10),
                    array('name' => 'Fred', 'age' => 20)), array('age' => '>'))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` > '10' ) OR
                                  ( `name` = 'Fred' AND `age` > '20' ));

If you want to set the default operator for all the columns, just pass it as the second parameter:

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('score' => '5', 'age' => 10),
                    array('score' => '15', 'age' => 20)), '>')
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `score` > '5' AND `age` > '10' ) OR
                                  ( `score` > '15' AND `age` > '20' ));

Multiple OR'ed conditions
-------------------------

You can add simple ORed conditions to the same WHERE clause using
``where_any_is``. You should specify multiple conditions using an
array of items. Each item will be an associative array that contains
a multiple conditions.

```php

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('name' => 'Joe', 'age' => 10),
                    array('name' => 'Fred', 'age' => 20)))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR ( `name` = 'Fred' AND `age` = '20' ));
```

By default, it uses the equal operator for every column, but it can be
overriden for any column using a second parameter:

```php

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('name' => 'Joe', 'age' => 10),
                    array('name' => 'Fred', 'age' => 20)), array('age' => '>'))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` > '10' ) OR ( `name` = 'Fred' AND `age` > '20' ));
```

If you want to set the default operator for all the columns, just pass it as
the second parameter:

```php

    <?php
    $people = ORM::for_table('person')
                ->where_any_is(array(
                    array('score' => '5', 'age' => 10),
                    array('score' => '15', 'age' => 20)), '>')
                ->find_many();

    // Creates SQL:
    SELECT * FROM `widget` WHERE (( `score` > '5' AND `age` > '10' ) OR ( `score` > '15' AND `age` > '20' ));
```
@lrlopez
Copy link
Contributor Author

lrlopez commented May 18, 2014

I know about the Pareto Principle that inspires Idiorm & Paris, but I think the implementation is simple enough for the benefits that you get.

This PR is also needed to support where_id_in() (#202) when compound keys are used.

treffynnon added a commit that referenced this pull request May 28, 2014
Multiple OR'ed conditions support
@treffynnon treffynnon merged commit de4a611 into j4mie:develop May 28, 2014
@periyasamy25
Copy link

How to write this query?
SELECT * FROM table WHERE (column1 LIKE '%value%' OR column2 LIKE '%value1%') AND column3='value3' order by column1 asc

Repository owner locked and limited conversation to collaborators Jul 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants