我有一个名为Recipe的课程如下:
class Recipe extends Page {
static $db = array(
"Title" => "Text"
);
static $many_many = array(
"RecipeTypes" => "RecipeType"
);
}
一个名为RecipeType的类如下:
class RecipeType extends DataObject {
static $db = array(
"Title" => "Text",
);
static $belongs_many_many = array(
"Recipes" => "Recipe"
);
}
对于我网站的搜索功能,我希望搜索结果是每个Recipe,其标题与搜索参数匹配,以及每个Recipe都有一个RecipeType,其标题与搜索参数匹配。
我尝试过以下方法:
return Recipe::get()->filterAny(array(
'Title:PartialMatch' => $searchString,
'RecipeTypes.Title:PartialMatch' => $searchString
));
但这只是返回其标题与搜索参数匹配的食谱;它没有返回任何食谱,其中一个食谱的食谱类型' Title属性与搜索参数匹配。
有什么想法吗?
答案 0 :(得分:1)
管理以使其使用以下内容:
$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;