BaronGrivet BaronGrivet - 2 months ago 16
PHP Question

Silverstripe ignoring NULL filter in get command

We have a Silverstripe project that uses the following logic to find duplicate records:

if ($queryString) {
$duplicate = SavedSearch::get()->filter(array(
'RentOrBuy' => $rentOrBuy,
'MemberID' => $member->ID,
'QueryString' => $queryString,
))->first();
} else {
$duplicate = SavedSearch::get()->filter(array(
'RentOrBuy' => $rentOrBuy,
'MemberID' => $member->ID,
'QueryString' => NULL,
))->first();
}


However this doesn't return the expected responses. When $queryString is NULL it doesn't return any objects - even when QueryString is set as "NULL" on matching fields in the database.

Any ideas about why this is happening/ what is going on?

Answer

It's fixed for 4.0 from the looks https://github.com/silverstripe/silverstripe-framework/pull/4196/commits/922d02f5356d5904debaf10003477cdd00306192

so for now..

if ($queryString) {
   $duplicate = SavedSearch::get()->filter(array(
    'RentOrBuy' => $rentOrBuy,
    'MemberID' => $member->ID,
    'QueryString' => $queryString, 
   ))
  ->first();
} else {
   $duplicate = SavedSearch::get()->filter(array(
     'RentOrBuy' => $rentOrBuy,
    'MemberID' => $member->ID
   ))
   ->where('QueryString IS NULL')
  ->first();
}

Be careful when using where, since it wont do any sql escaping. Here its fine but always use the filter when passing in user data.