Question

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?

Was it helpful?

Solution

Managed to get it working with the following:

$recipes_from_title = Recipe::get()->filter('Title:PartialMatch', $searchString);
$recipes_from_diet = RecipeType::get()->filter('Title:PartialMatch', $searchString)->relation('Recipes'); 

$matching_recipes = new ArrayList();

foreach($recipes_from_title as $recipe_from_title) {
    $matching_recipes->push($recipe_from_title);
}

foreach($recipes_from_diet as $recipe_from_diet) {
    $matching_recipes->push($recipe_from_diet);
}

return $matching_recipes;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top