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

Page Relation question


Go to End


2 Posts   2139 Views

Avatar
wewo

Community Member, 7 Posts

21 February 2009 at 10:56am

Edited: 21/02/2009 7:53pm

Hello,

In my project are three page types planed:

Artist, Location,Concert

The Concert has_one Location and many_many Artists, rather simple.
After a /db/build/?flush=1 my database has the extra fields for the $db variables end a table named "concert_artists" with ID (pk), ConcertID and ArtistID.

When I create a new Artist and assign some concerts the data gets stored in the db. The other way round, assigning some artists and a location to the concert fails without any error :(

Code:

<?php
/* Artist.php */

class Artist extends Page
{
static $db = array (
'Instrument' => 'Varchar',
'Link' => 'Varchar'
);
static $allowed_children = "none";
static $can_be_root = false;
static $belongs_many_many = array ('Concerts' => 'Concert' );
static $has_one = array( 'Photo' => 'Image');
function getCMSFields()
{

$fields = parent::getCMSFields();
$concertsList = DataObject::get('Concert');
if (!is_null($concertsList)) // catch cms error, if no concert exits
$fields->addFieldToTab('Root.Content.Concerts', new CheckboxSetField('Concerts', '', $concertsList));
$fields->addFieldToTab('Root.Content.Main', new TextField('Name') );
$fields->addFieldToTab('Root.Content.Main', new TextField('Instrument'));
$fields->addFieldToTab('Root.Content.Main', new TextField('Link'));
return $fields;
}
}

class Artist_Controller extends Page_Controller{}
?>

<?php
/* Location.php */
class Location extends Page {

static $db = array(
'ZIP' => 'Int',
'City' => 'Text',
'Langitude' => 'Float',
'Longitude' => 'Float',
'Country' => 'Text',
);

static $belongs_many_many = array ( 'Concerts' => 'Concert');

function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Details', new TextField('ZIP'));
$fields->addFieldToTab('Root.Content.Details', new TextField('City'));
$fields->addFieldToTab('Root.Content.Details', new TextField('Country'));
$fields->addFieldToTab('Root.Content.Details', new TextField('Langitude'));
$fields->addFieldToTab('Root.Content.Details', new TextField('Longitude'));
return $fields;
}
}

class Location_Controller extends Page_Controller { }
?>

<?php
/* Concert.php */
class Concert extends Page
{
static $db = array (
'EventDate' => 'Datetime',
'Name' => 'Text',
'Description' => 'HtmlText',
);

static $many_many = array ( 'Artists' => 'Artist' );
static $has_one = array('Location' => 'Location');
static $default_parent = 'ConcertsHolder';
static $can_be_root = false;
static $allowed_children = "none";
function getCMSFields($cms)
{

$fields = parent::getCMSFields($cms);
$artistList = DataObject::get('Artist');
if (!is_null($artistList))
$fields->addFieldToTab('Root.Content.Artists', new CheckboxSetField('Artist', '', $artistList));

$locationList = DataObject::get('Location');
if (!is_null($locationList))
$fields->addFieldToTab('Root.Content.Location', new CheckboxSetField('Location', '', $locationList));

$fields->addFieldToTab('Root.Content.Main', new PopupDateTimeField('EventDate', 'Event Date'), 'Content');
return $fields;
}
}

class Concert_Controller extends Page_Controller{}
?>

Do you find some mistake? Any help would be nice!

Bye,
Wewo
Ps: I'm using SS 2.2.3 with fix for RelationComplexTable.js

Avatar
wewo

Community Member, 7 Posts

22 February 2009 at 8:53am

Found the solution to my question in the meantime:

class Concert extends Page
{
....
static $many_many = array ( 'Artists' => 'Artist' );
...

$fields->addFieldToTab('Root.Content.Artist', new CheckboxSetField('Artists', '', $artistList)); // there was an *s* missing by Artists
}

class Artist extends Page
{
...
static $belongs_many_many = array ('Concerts' => 'Concert' );
}