I am trying to do something very similar for the news site I am building but haven't been able to get a successful result when following along with this or other similar examples.
I have a many to many relationship between the below two .php files.
Articles can belong to multiple Categories.
A Category can include multiple Articles.
NewsArticlePage.php
class NewsArticlePage extends Page {
static $many_many = array(
'Categories' => 'Category'
);
...
Category.php
class Category extends DataObject {
...
static $belongs_many_many = array(
'Articles' => 'NewsArticlePage'
);
I have one sample article built now, an instance of the NewsArticlePage page type, and using a checkbox form I added to the CMS (following the [url-http://doc.silverstripe.org/recipes:many_many-example/]many_many recipe) I assigned the article to a category with the ID of "1" and the title of "TopNews".
I want to display on my home page the $Title and $Content.FirstParagraph of whatever article is currently assigned to the "TopNews" category, which, as I said, right now is my sample article.
The home page is of the "Page" page type and I'm planning to call up the "TopNews" article data via a Control statement within an Include.
I've tried many approaches but keep running into a wall when it comes to retrieving the "TopNews" article's data. My understanding is that I need to add a function within my NewsArticlePage controller where I do some variation of "return DataObject:get..."
If someone could point me in the right direction I'd greatly appreciate it. This one issue has really been holding me back.
Thanks,
Steve