3070 Posts in 869 Topics by 651 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 776 Views |
-
Filtering DataOject::get

29 April 2011 at 10:39am Last edited: 29 April 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 {
}?>
-
Re: Filtering DataOject::get

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
-
Re: Filtering DataOject::get

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.
| 776 Views | ||
|
Page:
1
|
Go to Top |


