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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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


1746 Views

Avatar
k0m0r

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 %>
  <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?