Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObject::get() returns null

Go to End



Community Member, 39 Posts

22 April 2010 at 7:05pm

Edited: 22/04/2010 7:10pm

Hi. I have a problem with searching through DataObjects.
My code looks like:

function search() {
 $phrase = Director::urlParam("ID");
 $filter = "`ParentPageID` = '".Director::currentPage()->ID."' AND `Content` LIKE '%".$phrase."%'";
 $items = DataObject::get('MyObject',$filter);
 $data = array( "Results" => $items );
 return $this->customise($data)->renderWith('SearchResults');

And the template:

<% if Results %>
<% else %>
  <p>No results</p>
<% end_if %>

When the $phrase is found, everything works perfect. But when the $phrase filter doesn't match any objects, the template goes blank.
Checking $items->Count() doesn't help, it looks like the renderer doesn't work when no objects are found.

What does DataObject::get() return when the filter param doesn't match anything?
How can I make this work?