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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Finding related child pages


Go to End


2 Posts   978 Views

Avatar
earpwald

Community Member, 2 Posts

16 June 2016 at 11:39am

Edited: 17/06/2016 2:03am

Hey Folks,

I'm relatively new to SilverStripe and I'm trying to create a site that has a Teams page and a Match Report page. There is a one to many relationship between the two with one Team able to have many MatchReports. Match Report is also a sub-page of Team only.

Now I've a few issues here but I think they are all related. On an individual match report page I want to have a teaser for the previous report for that Team. Now I can sort it so that the reports come in descending order but for the life of me I cannot find out how to narrow the field to just the reports for that one team. There will be multiple teams, each with their own subsection of reports, and each report should only find the other reports for that team. I've tried a few different options to no avail so I'm hoping that someone can shed light on this. The latest code is below. It doesn't break but it doesn't work either.

class MatchReport extends Page {
	private static $can_be_root = false;

	private static $db = array(
		'Author' => 'Varchar',
		'Teaser' => 'Text',
		'Date' => 'Date',
	);

	private static $has_one = array (
		'Image' => 'Image',
		'Team' => 'Team',
	);
....

class MatchReport_Controller extends Page_Controller {
	public function PreviousArticle() {
		return MatchReport::get()
			->filter(array('Date:LessThan' => $this->Date, 'TeamID' => $this->Parent()->ID ))
			->sort('Date', 'DESC')
			->limit(1);
	}
}

class Team extends Page {
	private static $can_be_root = false;

	private static $allowed_children = array('MatchReport');

	private static $db = array (
	);

	private static $has_many = array(
		'Results' => 'MatchResult',
		'TrainingTimes' => 'TrainingTime',
		'Fixtures' => 'Fixture',
		'MatchReports' => 'MatchReport',
	);
.....

Any advice or help would be greatly appreciated. Thanks!

Avatar
earpwald

Community Member, 2 Posts

17 June 2016 at 2:01am

Ok so I've managed to create a work around. I'm not sure if it's because Match Report is a sub-page to Team or because I've somehow messed up the table creation, but despite having a $has_one reference to Team, the Match Report didn't save the TeamID to the database.

To get around this I've implemented the following code. I do prefer to use best coding practices, so if this is horribly wrong please let me know.

protected function onAfterAfterWrite() {
		parent::onAfterAfterWrite();
		if ($this->TeamID() == 0)
		{
			$this->TeamID() == $this->Parent->ID;
		}
	}

Cheers,
Earp