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.

General Questions

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

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

DataObject::get() returns null


Reply

1319 Views

Avatar
k0m0r

22 April 2010 at 7:05pm (Last edited: 22 April 2010 7:10pm), Community Member, 39 Posts

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 %>
<p>Results</p>
<% 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?