21489 Posts in 5783 Topics by 2622 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 985 Views |
-
Getting id's from a many to many relation

10 September 2009 at 7:41am
Hello guys,
Need a little help getting some IDs from a relation table.
The below code is what i use for a home page. In the administration you can now select multible pages, and the IDs are saved to the relation table "ForsidePage_PreviewList".
i need to use the IDs from this table in my filter in "ForsidePage_Controller" in the function "showPreviews", and not the hardcoded ...".ID IN (8,9,10)" ...How can this be done?
class ForsidePage extends Page {
static $db = array();static $has_one = array();
public static $many_many = array(
'PreviewList' => 'SiteTree'
);function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Previews', new TreeMultiselectField( 'PreviewList', 'Preview Sites', 'SiteTree'));return $fields;
}
}class ForsidePage_Controller extends Page_Controller {
function showPreviews() {
$myIds = "";// Query to get IDs from the relation table
$records = DataObject::get("WithPreviewPage", "`WithPreviewPage`.ID IN (8,9,10)");
return ($records);
}}
-
Re: Getting id's from a many to many relation

11 September 2009 at 12:19am Last edited: 11 September 2009 12:21am
What you're looking for is a method on the relational getter called getIdList(). To get an array of the IDs of the relation, simply call that method like so (on your controller):
$this->dataRecord->getManyManyComponents('PreviewList')->getIdList();
Then you can just implode the array into a comma separated ID string ready for your SQL query.
Sean
| 985 Views | ||
|
Page:
1
|
Go to Top |


