Can you add support for IS and IS NOT operators in the Query::where() function in the PHP libraries?

Can you add support for IS and IS NOT operators in the Query::where() function in the PHP libraries?

caleb.millercaleb.miller Posts: 1Questions: 1Answers: 0

I had trouble creating where statement looking for null values. I could not find any documentation on it, and had to look into the the Query class code to find out that when the value is null, then the '=' operator is replaced with 'IS', and any other operator is replaces with 'IS NOT'. Therefore, if you pass the value 'IS', it will be replaced with IS NOT. Can you add support for the 'IS' operator, or add documentation on how to test null values? The current code is:

if ( $value === null ) {
    // Null query
    $this->_where[] = array(
        'operator' => $type,
        'group'    => null,
        'field'    => $this->_protect_identifiers($key),
        'query'    => $this->_protect_identifiers($key) .( $op === '=' ?
            ' IS NULL' :
            ' IS NOT NULL')
    );
}

Adding || $op === 'IS' after $op === '=' shouldn't break anything that I can tell, and would make the where statements easier to use.

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    edited June 2018

    It always can - you just need to give null as the value - although I think (off the top of my head) you need to do it in a closure function:

    Editor::inst( ... )
      ...
      ->where( function ( $q ) {
        $q->where( 'field1', null );
        $q->where( 'field2', null, '!=' );
      } )
      ...
    

    edit Sorry! I jumped ahead and missed the point. Yes I see what you are saying. I think adding documentation for this is the way to do it.

    Allan

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    There is information about using NULL values as part of the condition available here. Does that cover what you are looking for?

    Thanks,
    Allan

This discussion has been closed.