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.

Connect With Other SilverStripe Members /

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

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

DataObject within DataObject


Reply


5 Posts   693 Views

Avatar
SilverPeed

Community Member, 13 Posts

17 September 2013 at 9:49pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

18 September 2013 at 9:03pm

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

Avatar
SilverPeed

Community Member, 13 Posts

18 September 2013 at 9:17pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

18 September 2013 at 9:21pm

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)

Avatar
SilverPeed

Community Member, 13 Posts

18 September 2013 at 9:32pm

Thanks,

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

Awesome