Jump to:

382 Posts in 210 Topics by 196 members

Connect With Other SilverStripe Members

SilverStripe Forums » Connect With Other SilverStripe Members » DataObject within DataObject

For all SilverStripe-related topics that don't fit into any of the categories above.

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

Page: 1
Go to End
Author Topic: 609 Views
  • SilverPeed
    Avatar
    Community Member
    12 Posts

    DataObject within DataObject Link to this post

    Hello somebody,

    I'm having trouble understanding the DataObject $hase_one relation ship. I'm using Silverstrip 3.05
    The following is what i'm trying to accomplish:
    I'm trying to create a page where i can add projects thru a GridField and present them thru a slide show. Every slide show has a variable number of pictures so i thought i would need a DataObject within a DataObject.

    I'm using three Page types.
    - Project (a page that has a relation with Slideshow)
    - Slideshow (a Dataobject witch has a description and title of the project)
    - Images (a DataObject witch has a relation with Project so i can add as many picturs to the slideshow)

    ProjectPage and Project works but i cant add as many pictures as i want.
    Below is my code example, i'm not using pictures yet, i'm just trying to understand howe it works.
    ----------------------------------------------------------------------------------------
    <?php
    class Project extends Page {
    static $has_many = array(
    'Slideshows' => 'Slideshow'
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
    $config = GridFieldConfig_RelationEditor::create();
    $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
    'Name' => 'Name',
    'Project.Title'=> 'Project'
    ));
    $SlideshowsField = new GridField(
    'Slideshows',
    'Slideshow',
    $this->Slideshows(),
    $config
    );
    $fields->addFieldToTab('Root.Slideshows', $SlideshowsField);
    return $fields;
    }
    }

    class Project_Controller extends Page_Controller {
    }

    -----------------------------------------------------------------------------------

    <?php
    class Slideshow extends DataObject {
    static $db = array(
    'Name' => 'Varchar',
    'University' => 'Varchar',
    );
    static $has_one = array(
    'Project' => 'Project'
    );
    static $has_many = array(
       'Images' => 'Images'
    );
    }

    ------------------------------------------------------------------------------------

    <?php
    class Images extends DataObject {
    static $db = array(
    'Something1' => 'Varchar',
    'Something2' => 'Varchar',
    );
    static $has_one = array(
    'Slideshow' => 'Slideshow'
    );
    }

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: DataObject within DataObject Link to this post

    You look to have set it up great and got the main relationships look setup correctly. Do you get a particular error message?

  • SilverPeed
    Avatar
    Community Member
    12 Posts

    Re: DataObject within DataObject Link to this post

    Thanks for your response Willr,

    I don't get an error but the "Images" DataObject does not show up in the GridField.
    I only see Slideshow. It looks like Images does nothing.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: DataObject within DataObject Link to this post

    Images should be shown when you click into a slideshow object in the grid field. Check to see if the Image GridField isn't on another tab (along the top right) and if not, perhaps define another getCMSFields for the SlideShow that includes a GridField for the image (though that should be automatically created)

  • SilverPeed
    Avatar
    Community Member
    12 Posts

    Re: DataObject within DataObject Link to this post

    Thanks,

    You where right, i did a flush(not shure if necessary) and it does show in another tab now.

    Awesome

    609 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.