Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

where A and B and ( C or D or F)

edited August 2014 in Framework

Is it possible to use "where" and "or_where" Model methods, in oder to make a where like in title?

Comments

  • There are a couple ways you can do it, but passing function is probably the best. You can pass multiple clauses to where() as an array, but the IDs of an array have to be unique, so that's limiting. For example, this works:

    ->where (array ('id' => 'about', 'title' => 'About'))
    

    But this doesn't:

    ->where (array ('id' => 'about', 'id' => 'contact'))
    

    The alternative is to pass a function like this:

    $res = Webpage::query ()
        ->where (function ($q) {
            $q->where ('id', 'about');
            $q->or_where ('id', 'contact');
        })
        ->or_where (function ($q) {
            $q->where ('access', 'public');
            $q->or_where ('access', 'member');
        })
        ->fetch ();
    

    This should produce the following SQL:

    select * from #prefix#webpage where (id = ? or id = ?) or (access = ? or access = ?)
    

    With parameters array ('about', 'contact', 'public', 'member').

  • edited August 2014

    I did not know about the use of "function".

    This significantly expands the possibilities; why not add an usage example on the "Class: Model" page?

    Thank you very much.

  • I should update the docs with more complex query examples. Btw, the latest docs are online at a temporary address for the new Elefant website (which is not quite finished yet):

    http://e2.campfireunion.com/

    Cheers!

  • I like this!

Sign In or Register to comment.