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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Getting id's from a many to many relation

Go to End

2 Posts   1516 Views


Community Member, 20 Posts

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);



Forum Moderator, 922 Posts

11 September 2009 at 12:19am

Edited: 11/09/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):


Then you can just implode the array into a comma separated ID string ready for your SQL query.