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

[Solved] many to many relationship on same page type, ie article page has some other related article


Go to End


5 Posts   1941 Views

Avatar
Ben_W

Community Member, 80 Posts

9 February 2011 at 8:40pm

In the past, I have done one to many or many to many relationship using hasmanycomplextablefield, ManyManyComplexTableField. However, is it possible to assign the same page type to itself to form a many to many relationship. For instance you create a page type named. 'Article page' then in its own class add many to many relationship.

<?php
/**
* Defines the ArticlePage page type
*/

class ArticlePage extends Page {
static $db = array(
'Headline' => 'Text',
'Summary' => 'Text'
);
static $has_one = array(

);
static $has_many = array(
'MyArticlePages' => 'ArticlePage',
'RelateToArticlePage' => 'ArticlePage'
);

function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Main', new TextField('Headline'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');

$tablefield = new ManyManyComplexTableField(
$this,
'MyArticlePages',
'ArticlePage',
array(
'Headline' => 'Page Headline',
'Summary' => 'Summary'
),
'getCMSFields_forPopup',
null,
'Headline'
);
$tablefield->setAddTitle( 'A Related Page' );

$tablefield->setPermissions(array(
));

$fields->addFieldToTab( 'Root.Content.Related', $tablefield );

return $fields;
}

}

class ArticlePage_Controller extends Page_Controller {

}
?>

I got the following error:

Fatal error: Cannot instantiate abstract class Object in /var/www/vhosts/isnt-test.com/httpdocs/sapphire/core/Object.php on line 229

Has anyone done this before? thank you for taking the time to read this.

Avatar
Ben_W

Community Member, 80 Posts

10 February 2011 at 12:20pm

I managed to make this work. the previous code mixed up with HasManyComplexTableField, my bad. For those of you might come cross the same issue. Here is the working code:

<?php
/**
* Defines the RegionChildPage page type
*/

class RegionChildPage extends Page {
static $db = array(
'Headline' => 'Text',
'Summary' => 'Text'
);
static $has_one = array(

);
static $many_many = array(
'MyRegionChildPages' => 'RegionChildPage'
);

static $belongs_many_many = array(
'RelateToRegionChildPage' => 'RegionChildPage'
);

function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Main', new TextField('Headline'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');

$tablefield = new ManyManyComplexTableField(
$this,
'MyRegionChildPages',
'RegionChildPage',
array(
'Headline' => 'Page Headline',
'Summary' => 'Summary'
),
'getCMSFields_forPopup',
null,
'Headline'
);
$tablefield->setAddTitle( 'A Related Page' );

$tablefield->setPermissions(array(
));

$fields->addFieldToTab( 'Root.Content.Related', $tablefield );

return $fields;
}

}

class RegionChildPage_Controller extends Page_Controller {

}
?>

Avatar
Ben_W

Community Member, 80 Posts

10 February 2011 at 12:57pm

Add this in as the filter if don't want the current region child page show up in the complexTableField as a option to link to itself.

"RegionChildPage.ID != $this->ID", //To the filter section.

Here is the full view of the ManyManyComplexTableField

$tablefield = new ManyManyComplexTableField(
$this,
'MyRegionChildPages',
'RegionChildPage',
array(
'Headline' => 'Page Headline',
'Summary' => 'Summary'
),
'getCMSFields_forPopup',
"RegionChildPage.ID != $this->ID",
'Headline'
);

Avatar
marc79

Community Member, 65 Posts

4 May 2011 at 8:13am

Hi,

Just searching around and found your post which helped me out with a problem I was having.

"RegionChildPage.ID != $this->ID",

I am using the above line opposite way, so that I only see certain information that has been assigned an ID, but I've since been looking for a way to add in NULL so that I can show items with a specific ID and those that are yet to be assigned and ID.

Do you know if this is possible? I've tried several variations along the line of the below. e.g. with or without " " or ' ' but with no luck.

"TrackProductID='$this->ID' or 'NULL'"

Any suggestions would be greatly received.

Thanks

Marc

Avatar
Ben_W

Community Member, 80 Posts

5 May 2011 at 5:56pm

Try this:
"TrackProductID=$this->ID OR TrackProductID IS NULL"