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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Highlight "category" instead of Parent page in menu


8 Posts   2214 Views


Community Member, 61 Posts

13 December 2009 at 12:32am

I've got several pages, let's say :

- AllRecipes
      - Recipe (A)
      - Recipe (B)
      - Recipe (C)
      - ...
- Category (a)
- Category (b)
- Category (c)
- ...

Each Recipe can belong to several Categories ; each Category can hold several Recipes ; I created a dataobject called "RecipeCategory" so that I can manage all the relationships between my Category Pages and my Recipe Pages.
If I click on "Category a" in my main navigation bar, it displays every title of all the recipes belonging to the category.

My problem is when I call the Recipe page itself.
The main navigation bar is coded this way :
<% control Menu(1) %>
<li><a href="$Link" class="$LinkingMode">$MenuTitle</a></li>
<% end_control %>
So the highlighted menu item is always "AllRecipes", since all my Recipes are children of the AllRecipes page...

I would like the Category (or even the categories, since it's a many-to-many relationship) to be the highlighted menu item, instead of the parent page.

What would be the best way to get to this ?
Thank you !



Community Member, 97 Posts

14 December 2009 at 8:50am

Edited: 14/12/2009 8:52am


It strikes me that you've taken the wrong route from the start here, actually. If I understand you correctly, you've created a class called RecipeCategory to capture the many-many relationship between Recipe and Category. You don't need to do this: SilverStripe will create the necessary database table itself.

See these:

  • [url][/url]
  • [url]Page 53 of the SS book[/url]

Assuming you want both Recipes and Categories to be in the site tree, you need something like this:

class Recipe extends Page {
static $many_many = array('Categories' => 'Category');

class Category extends Page {
static $belongs_many_many = array('Recipes' => 'Recipe');

For the navigation, if you want to show recipes for a particular category, you can't rely on the site tree since you have, as you say, only your Recipes page with its child Recipe pages. Instead, you need to get hold of the appropriate Category object, perhaps using <% Page(categories) %> - assuming the URLSegment of your categories page is categories - and in the template, use <% control Recipes %> to iterate through the recipes.

You'll need to use ManyManyComplexTableField to get the CMS to manage your many-many relationship. See the tutorial in the link above.



Community Member, 61 Posts

14 December 2009 at 10:07pm

Toby, you've been really helpful, and I think I understand Silverstripe a little better thanks to you.
Actually, I think I stuck to much to the tutorial #5

So, what I did first was get rid of the useless dataobject. And then :

In Recette.php :

class Recette extends Page {
static $many_many = array(
'Categories' => 'Categorie'

function getCMSFields() {
$fields = parent::getCMSFields();

$categoriesTablefield = new ManyManyComplexTableField(
    'MenuTitle' => 'MenuTitle'
$categoriesTablefield->setAddTitle( 'Catégories' );
    $categoriesTablefield -> setPermissions(array());
$fields->addFieldToTab( 'Root.Content.Categories', $categoriesTablefield );

return $fields;

In Categorie.php :

class Categorie extends Page {
static $belongs_many_many = array(
'Recettes' => 'Recette',

And I added this function to my HomePage controller to retrieve all the recipes :

function Recettes() {
      return DataObject::get("Recette");   

And it works well : I can select many categories every time I create a new "recette" (recipe) ; and every recipe is child of a category.

Now, is it possible to go a little further, and add a ManyManyComplexTableField to the "Categorie" pages in the CMS, to select several existing Recipes every time I create a new category ?

Thanks a lot !


Community Member, 97 Posts

14 December 2009 at 10:13pm



Community Member, 61 Posts

15 December 2009 at 4:40pm

Hello again

So, I tried to add a ManyManyComplexTableField to the "Categorie" pages in the CMS, but it doesn't work : since it's on the belongs_many_many side, I've got the same issue as here : [url][/url]

Any solution ?

Thanks again !


Community Member, 97 Posts

15 December 2009 at 6:57pm

Seems there's a bug in SilverStripe:

This is such a basic thing... but then SilverStripe is still very young and there are many other basic things that don't work.

This should be possible: as a poster points out, SiteTree is a subclass of DataObject, and in that sense is nothing special. Certainly I wouldn't recommend using many_many on both sides of a relationship: you're not supposed to do that, as I understand it. As the book points out, the only reason both many_many and belongs_many_many exist is to for the ORM to determine what to call the table (eg RecipeCategory or CategoryRecipe).



Community Member, 61 Posts

15 December 2009 at 7:42pm

And using $many_many at both sides simply doesn't work : the second ManyManyComplexTableField doesn't take into account the relationships set up in the first one.


Community Member, 10 Posts

9 February 2010 at 1:33pm

I'm in the same situation. Are there any solutions for this? I have People and Groups and people can be in multiple groups and I want to be able to manage the relations in ModelAdmin from both sides. ie. When creating a group I can select the people off that are in it, or when creating a person i can select the groups they are in... Is there no easy solution in SS? seems like this should the standard behavior of $many_many relationships in ModelAdmin no?