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:

Data Model Questions /

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

Complex search functionality


Go to End


801 Views

Avatar
jordanmk

Community Member, 3 Posts

21 March 2014 at 8:08pm

Edited: 21/03/2014 11:07pm

I have a class called Recipe as follows:

    class Recipe extends Page {
    
    	static $db = array(
    		"Title" => "Text"
    	);
    
    	static $many_many = array(
    		"RecipeTypes" => "RecipeType"
    	);
    }

And a class called RecipeType as follows:

    class RecipeType extends DataObject {
    	static $db = array(
    		"Title" => "Text",
    	);

    	static $belongs_many_many = array(
    		"Recipes" => "Recipe"
    	);
    }

For my site's search functionality, I want the search results to be every Recipe which has a Title that matches the search parameter, as well as every Recipe which has a RecipeType whose Title matches the search parameter.

I've tried the following:

    return Recipe::get()->filterAny(array(
    	'Title:PartialMatch' => $searchString,
    	'RecipeTypes.Title:PartialMatch' => $searchString
    ));

But that is only returning Recipes whose Title matches the search parameter; it's not returning any Recipes where one of the Recipe's RecipeTypes' Title property matches the search parameter.

Any ideas?