0
votes

I have the following code:

$data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC");
$f1 = new JSONDataFormatter(); 
return $f1->convertDataObjectSet($data); 

However the response is:

{"totalSize":null,"items":[]}

There are definitely records in the DataObject, as if I do:

$data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC");
foreach($data as $dataobj){
print_r($data);
}

I can see the data for all the records.

1

1 Answers

1
votes

The ORM doesn't actually execute a query until it is iterated which explains why results exist when looping over $data with foreach but not by just writing the get(...) statement.

A solution is to use the toArray() method on your DataList which will then execute the query and you'll have the results in an Array:

$data = DataObject::get('Property',"SoftDelete=0 AND Bedrooms >= ".$minBeds." AND Price<='". $maxPrice."'","Price ASC")->toArray();

(Note that this might/will throw an error is there is no results, so might need to check with ->count() first)

convertDataObjectSet() seems to take an SS_List as param, so you might have to convert $data (not sure) like so $data = ArrayList::create( $data ).