How to use OR in idiorm SQL query in PHP?

2.2k Views Asked by At

I am developing an application using idiorm(https://github.com/j4mie/idiorm) for fetching data from database.

Here is my piece of code:

$messages = Model::factory('Messages')->where('from_user_id','1')->find_many();

I want to include an or statement that where->('to_user_id','1')

How can I put this as an OR statement in my Idiorm query?

2

There are 2 best solutions below

0
RiggsFolly On BEST ANSWER

This looks like what the manual is telling you to do for an OR condition

$messages = Model::factory('Messages')
                   ->where_any_is(
                                  array(
                                         array('from_user_id' => 1),
                                         array('to_user_id' => 1)
                                       )
                                  )    
                   ->find_many();
0
Mr. Llama On

I'm not familiar with idiorm, but looking at the source code you should be able to use where_raw to build your own where clause:

    /**
     * Add a raw WHERE clause to the query. The clause should
     * contain question mark placeholders, which will be bound
     * to the parameters supplied in the second argument.
     */
    public function where_raw($clause, $parameters=array()) {
        return $this->_add_where($clause, $parameters);
    }

Something like ->where_raw('from_user_id = ? OR to_user_id = ?', array(1, 1))