Lucky Luke Lucky Luke - 6 months ago 4x
PHP Question

Parse PHP SDK - return records with NULL or undefined value

I have some records stored through Parse PHP SDK.
Among the fields there is a Date field which is initialized with an undefined value.

On the go this value may change either to a real date or a null value, in case I set a date and then want to "unset" it. What I am doing is setting it as null.

The problem is that when I want to filter the set dates and all the others (in my case both undefined and null values should be the same, i.e. "unset") I try a query filter similar to the following:

$query->equalTo("mydatefield", NULL);

The weird thing about it is that it only returns the undefined values, not the NULL ones.

On the other hand when I use

$query->notEqualTo("mydatefield", NULL);

the desired records are all returned just fine.

Any idea how to accomplish getting both null and undefined values?

Thanks in advance.


You are correct, it doesn't seem to work with the PHP library. The same thing works fine with the Javascript library.

You can accomplish what you want though by excluding the values that are not null (or undefined). Here is an example:

use Parse\ParseQuery;       

//fetch objectIds of all data that is not null
$query = new ParseQuery("tabledata");
$query = $query -> notEqualTo("mydatefield", null);
$results = $query -> find();
$keys = array();

for ($i=0; $i<count($results); $i++){
    array_push($keys, $results[$i]-> getObjectId());

//now fetch all records but the records with the null column
$query2 = new ParseQuery("tabledata");
$query2 = $query2 -> notContainedIn("objectId", $keys);
$results2 = $query2 -> find();