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

Filtering DataOject::get


Go to End


4 Posts   1482 Views

Avatar
VictorH

Community Member, 29 Posts

29 April 2011 at 10:39am

Edited: 29/04/2011 10:40am

I have a Profile class and a CaseStudy class. I've created a man_many relationship between the two and I'm trying to display the CaseStudies that belong to the Profile page. So far I have:

function ProfileCaseStudies() {
		return DataObject::get('CaseStudy', "", '', '', '');
	}

which pulls all the CaseStudies. How do I filter it so its only pulling the CaseStudies that belong to the specific Profile.

Full Code

Profile.php

<?php
 
class Profile extends Page {
	static $db = array(
		'Summary' => 'Text'
	);
	
	static $has_one = array(
		'Photo' => 'Image'
	);
	
	static $many_many = array(
		'CaseStudies' => 'CaseStudy'
	);
 
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
		$fields->addFieldToTab('Root.Content.Main', new ImageField('Photo'), 'Content');
		
		
		$profileList = new ManyManyComplexTableField(
			$this,
			'CaseStudies',
			'CaseStudy',
			array(
				'Title' => 'Title'
			),
			'getCMSFields_forPopup'
		);
		
		$fields->addFieldToTab('Root.Content.Categories', $profileList);
		
		return $fields;
	}
}
 
class Profile_Controller extends Page_Controller {
	function ProfileCaseStudies() {
		return DataObject::get('CaseStudy', "", '', '', '');
	}
}
 
?>

CaseStudy.php

<?php
 
class CaseStudy extends Page {
	static $db = array(
		'Summary' => 'Varchar',
		'Featured' => 'Boolean'
	);
	
	static $many_many = array(
		'Categories' => 'Category'
	);
	
	static $belongs_many_many = array(
		'Profiles' => 'Profile'
	);
	
	static $default_parent = 'CaseStudyCategory';
 
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
		$fields->addFieldToTab('Root.Content.Main', new CheckboxField('Featured'), 'Content');
		
		
		$categoryList = new HasManyComplexTableField(
			$this,
			'Categories',
			'Category',
			array(
				'Name' => 'Name'
			),
			'getCMSFields_forPopup'
		);
		
		$fields->addFieldToTab('Root.Content.Categories', $categoryList);
		
		return $fields;
	}
}
 
class CaseStudy_Controller extends Page_Controller {
}
 
?>

Avatar
JonoM

Community Member, 130 Posts

29 April 2011 at 11:15am

Hi Victor,

For something like that I don't think you need to write a new function - if you call the name of the relationship in the ProfilePage template it should return the related CaseStudies as a dataobjectset.

so if you have $many_many = array( 'CaseStudies' => 'CaseStudy') on your Profile class try this in your Profile.ss

<% control CaseStudies %>
//Case study properties, $Title etc.
<% end_control %>

If you want to use that dataobjectset in a custom function you could use $this->CaseStudies() from your controller

Cheers

Avatar
VictorH

Community Member, 29 Posts

29 April 2011 at 11:19am

Thank you so much JonoM it was killing me and it was such a simple solution that flew right by me.

Avatar
JonoM

Community Member, 130 Posts

29 April 2011 at 11:59am

You're welcome, I know that feeling!