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.

Customising the CMS /

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

Search Form, Results, Custom Fields


Go to End


17 Posts   7018 Views

Avatar
stew

Community Member, 30 Posts

15 July 2010 at 2:32am

Hey guys,

I'm 90% through building a site using Silverstripe (2.4), it's coming along well but I've hit a problem.

I need the search form to search inside a few custom fields in "DatabasePage" - Address, Category, Description - and as I've discovered it's not currently possible to search the custom fields.

I also need to display a few of the custom fields in the results page.

So just a little bit stuck, does anyone know how to solve these two problems?

Thanks,

Stewart

Avatar
swaiba

Forum Moderator, 1899 Posts

15 July 2010 at 3:09am

Are you talking ModelAdmin?

Avatar
stew

Community Member, 30 Posts

15 July 2010 at 3:15am

Edited: 15/07/2010 3:15am

No, just the normal CMS/Pages bit.

I have had a look at ModelAdmin and might spend a few hours tomorrow seeing if it's easier to do it that way.

Avatar
swaiba

Forum Moderator, 1899 Posts

15 July 2010 at 3:16am

Ok, sorry can't be of much more help then...

Avatar
sonet

Community Member, 33 Posts

15 July 2010 at 5:03am

You can check the http://silverstripe.org/sphinx-module module.

Avatar
stew

Community Member, 30 Posts

15 July 2010 at 10:38pm

After speaking with the client they want to keep their current shared hosting plan so Sphinx is out of the window sadly. Thanks for the suggestion though, looked almost perfect.

I've managed to work out a slightly awkward way of getting search working - use the Meta > Keywords bit to add in "Tags"... however I can't pull out the custom fields of each search result, has anyone managed to do this?

Avatar
swaiba

Forum Moderator, 1899 Posts

15 July 2010 at 10:44pm

Edited: 15/07/2010 10:44pm

ahhhh I mis understood... might something like this help...

in Page.php

/**
 * Process and render search results
 */
function results($data, $form){
	$data = array(
	'Results' => $form->getResults(),
	'Query' => $form->getSearchQuery(),
	'Title' => 'Search Results'
	);



	/////////////////////////////////////////////////////////////
	// Start Custom

	$arrSearchQuery = explode(' ',$form->getSearchQuery());
	$strSearchQuery = '%'.implode('%',$arrSearchQuery).'%';

	$dos = DataObject::get('YOURDATAOBJECT',"FIELD LIKE '".$strSearchQuery."'");

	//make our our result set
	$dosNewRestults = new DataObjectSet();

	if ($dos)
	{
		$arr = $dos->toArray();
		foreach ($arr as $do)
		{
			$p = new Page();
			$p->Title = $do->SOMEFIELD;
			$p->URLSegment = 'SOMEURL?POSTVAR='.$do->SOMEFIELD;;
			$p->Content = $do->SOMEFIELD;;
			$p->Name = $do->SOMEFIELD;;
			$p->Relevance = 1;
			$p->CanViewType = 'Anyone';

			$dosNewRestults->push($p);
		}
	}


	//strip out the html from the results and append them to the results
	//above - so that the ones with things to buy are first
	if ($data['Results']->Count() > 0)
	{
		$arr = $data['Results']->toArray();
		foreach ($arr as $do)
		{
			$do->Content = strip_tags($do->Content);
			$dosNewRestults->push($do);
		}
	}

	$data['Results'] = $dosNewRestults;
	// End Custom
	/////////////////////////////////////////////////////////////



	return $this->customise($data)->renderWith(array('Page_results', 'Page'));
}

It ain't the best, but it might help.

Barry

Avatar
stew

Community Member, 30 Posts

15 July 2010 at 10:45pm

Thanks Barry!

I'll take a look at it this afternoon and see what happens :)

Go to Top