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.

Connect With Other SilverStripe Members /

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

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

DataObject within DataObject


Go to End


5 Posts   1903 Views

Avatar
SilverPeed

Community Member, 22 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, 5523 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, 22 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, 5523 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, 22 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