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.

Form Questions /

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

SearchForm returning strange results


Go to End


3 Posts   1704 Views

Avatar
MarcusDalgren

Community Member, 288 Posts

8 July 2009 at 12:28am

Hello.

I've just followed the tutorial for site search and I am getting some strange results from the search.
The strange thing is that the results I get back don't act as if they're the class they're supposed to be.

For example, I have a ProductPage that extends Page which, among other things, contains an Image through a has_one.

When I do a search on one of the products I get a DataObjectSet back with a ProductPage DataObject and as far as I can see, all the data for the page is correct. However if I do an attempt to get the image I get an empty image object, not the image related to the product.

I found a way around this by looping through the search results and doing

$results = $form->getResults();
$searchData = new DataObjectSet;
foreach ($results as $result) {
	$searchData->push(DataObject::get_by_id($result->ClassName, (int)$result->ID));
}
$data['Results'] = $searchData;

And then returning $searchData to the template. Now I get access to the correct images and stuff. However this is essentially fetching the same results twice so if there's an easier way to make the search function return the right stuff I would be very happy if someone could tell me what to do.

Kindly, Marcus.

Avatar
snaip

Community Member, 181 Posts

25 February 2010 at 10:52pm

hi

could you show the complete search code and template code ?

Avatar
snaip

Community Member, 181 Posts

1 March 2010 at 1:05am

ok i got it

	function results($data, $form){
	
		if($form->getSearchQuery() == NULL || $form->getSearchQuery() == 'Znajdź produkt') {
			$data = array(
				'Results' => '',
				'Query' => '',
				'Title' => 'Znalezione wyniki'
			);			
		} else {			
			$results = $form->getResults();
			$searchData = new DataObjectSet;
			foreach ($results as $result) {
				$searchData->push(DataObject::get_by_id($result->ClassName, (int)$result->ID));
			}	
			$data = array (
				'Results' => $searchData,
				'Query' => $form->getSearchQuery(),
				'Title' => 'Znalezione wyniki'				
			);
		}
		
	  	return $this->customise($data)->renderWith(array('Wyszukaj', 'Wyszukaj')); 
	}