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.

Data Model Questions /

difficulty with relations and controlling them in ModelAdmin


Reply


6 Posts   1186 Views

Avatar
theoldlr

Community Member, 103 Posts

24 November 2009 at 10:51am

I want to make a carousel module. To me this means 2 DataObjects (Slide and Carousel) and a page type (CarouselPage) that will contain the Carousel.

A carousel will have a title to describe it (i.e. Pictures from Vacation) and any number of slides (I'm hoping to be able to select them from a ComplexTableField), while a slide will have a title (i.e. day 1 on the beach) and html code and/or an image.

In terms of relations, carousel:
static $has_many = array(
'MySlides' => 'Slide'
);

slide:

static $belongs_many_many = array(
'Carousels' => 'Carousel'
);

Would this be the best way to relate these two? Would it be smarter to just have a Carousel Page type and a Slides DataObject and skip the carousel DataObject? Do the slides really need a relation back to the Carousel? I'd like to hear from those more experienced.

Thanks!

Avatar
theoldlr

Community Member, 103 Posts

25 November 2009 at 6:11am

I'm sure my relation is still not right because my HasManyComplexTableField does not have any checkboxes. Can someone please point me in the right direction?

class Carousel extends DataObject {

static $db = array (
'Title' => 'Varchar'
);

static $many_many = array(
'MySlides' => 'Slide'
);

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

$tablefield = new HasManyComplexTableField (
$this,
'MySlides',
'Slide',
array(
'Title' => 'Title'
),
'getCMSFields_forPopup'
);
$tablefield->setAddTitle( 'A Slide') ;
$fields->addFieldToTab('Root.Main', $tablefield);

return $fields;
}
}

class Slide extends DataObject {
static $db = array(
'Title' => 'Varchar',
'Code' => 'HTMLText'
);

static $belongs_many_many =array(
'Carousels' => 'Carousel'
);

Avatar
wee-man

Community Member, 21 Posts

25 November 2009 at 8:44am

Hi,
you have one Carousel with many Slides, so this is an 1:n relation.

To implement this relation in Silverstripe you have to:

In class Carousel:

static $has_many = array(
'MySlides' => 'Slide'
);

and in class Slide:

static $has_one =array(
'Carousel' => 'Carousel'
);

I hope this will help ;)

Avatar
theoldlr

Community Member, 103 Posts

25 November 2009 at 8:48am

Thank you for responding, wee-man

Doesn't the relation that you mentioned mean that you cannot use the same slide in more than 1 carousel?

Avatar
wee-man

Community Member, 21 Posts

25 November 2009 at 9:01am

jep - that's right.
In this case you can associate a slide to only one carousel.

If you want to use a slide in more than one carousel then you need a n:m relation.

For that you have to use

$many_many = array("MySlides" => "Slide"); //in class Carousel

$belongs_many_many = array("MyCarousels" => "Carousel"); //in Slide

Avatar
theoldlr

Community Member, 103 Posts

25 November 2009 at 9:30am

Edited: 25/11/2009 11:35am

I see these relationships working properly now. Thank you for your help, wee-man.

What I need to focus on next is changing this Carousel ModelAdmin portion of the CMS to be more user friendly for editing the different dataObjects.

Ideally, I would like to be able to select a tab on the left side (carousel or slide) and have a list similar to the site tree appear underneath. Then modify the objects on the right. Is ModelAdmin the right tool for this job, or would I be better off with a different approach?

I appreciate any advice or references that could be of help.