Stefan Brinkmann
Stefan Brinkmann

Reputation: 985

Howto select an where - if - in with Kohana ORM?

Im trying to do this in with Kohana ORM but have no idea how to do that:

SELECT t.name
FROM t
WHERE IF(t.s1_id = 1, t.s2_id IN (2,3,4), t.s1_id IN (2,3,4))

Upvotes: 0

Views: 241

Answers (2)

kero
kero

Reputation: 10638

I haven't worked with this myself but with floww's transformation, this should do the trick

$model = ORM::factory('t')->and_where_open()
    ->where('s1_id', '=', '1')
    ->where('s2_id', 'IN', array(2,3,4))
    ->and_where_close()
    ->or_where('s1_id', 'IN', array(2, 3, 4));

If you expect one result, use $model->find() now and you can access the name via $model->name.

If you expect multiple results, use $model->find_all() and iterate over it, getting all the ->names


As you can see in the API browser, ORM doesn't support IF statements. If you don't want a hardcoded solution like the one posted, I think you'd have to add this functionality yourself.

Upvotes: 1

jko
jko

Reputation: 2098

Try this:

SELECT t.name FROM t WHERE (t.s1_id = 1 and t.s2_id IN (2,3,4) ) or ( t.s1_id IN (2,3,4) )

Upvotes: 1

Related Questions