Skip to content

where_id_in() for selecting multiple records by primary key#202

Merged
treffynnon merged 2 commits intoj4mie:developfrom
lrlopez:where_id_in-2
May 28, 2014
Merged

where_id_in() for selecting multiple records by primary key#202
treffynnon merged 2 commits intoj4mie:developfrom
lrlopez:where_id_in-2

Conversation

@lrlopez
Copy link
Contributor

@lrlopez lrlopez commented May 18, 2014

This PR allows selecting multiple records by passing an array of primary keys.

I.e.:

    <?php
    $people = ORM::for_table('person')
                ->where_id_in(array(1, 5))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `person` WHERE id IN (1,5);

It also supports compound primary keys:

    <?php
    $people = ORM::for_table('person_profile')
                ->use_id_column(array('person_id', 'profile_id'))
                ->where_id_is(array(
                    array('person_id' => '5', 'profile_id' => 10, 'bogus' => 'whatever'),
                    array('person_id' => '10', 'profile_id' => 10)))
                ->find_many();

    // Creates SQL:
    SELECT * FROM `person_profile` WHERE (( `person_id` = '5' AND `profile_id` = '10' ) OR
                                  ( `person_id` = '10' AND `profile_id` = '10' ));

Closes #199

lrlopez added 2 commits May 18, 2014 20:39
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 lrlopez changed the title New feature: where_id_in() for selecting multiple records by primary key where_id_in() for selecting multiple records by primary key May 18, 2014
treffynnon added a commit that referenced this pull request May 28, 2014
where_id_in() for selecting multiple records by primary key
@treffynnon treffynnon merged commit 9b3b9ca into j4mie:develop May 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants