Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » difficulty with relations and controlling them in ModelAdmin

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

Page: 1
Go to End
Author Topic: 1167 Views
  • theoldlr
    Avatar
    Community Member
    103 Posts

    difficulty with relations and controlling them in ModelAdmin Link to this post

    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!

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: difficulty with relations and controlling them in ModelAdmin Link to this post

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

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: difficulty with relations and controlling them in ModelAdmin Link to this post

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

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: difficulty with relations and controlling them in ModelAdmin Link to this post

    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?

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: difficulty with relations and controlling them in ModelAdmin Link to this post

    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

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: difficulty with relations and controlling them in ModelAdmin Link to this post

    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.

    1167 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.