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.

Customising the CMS /

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

SS3 Get related has_one object in data object


Go to End


3 Posts   3341 Views

Avatar
donkikong

Community Member, 3 Posts

6 July 2013 at 2:53am

Edited: 06/07/2013 2:53am

Hello there!

I'm trying to build a simple image gallery.

I created an GalleryImage DataObject that has a has_one relation to GalleryPage. I would like to be able to specify a folder name that is formed from the id and / or name of the related GalleryPage.

I know I can set the folder with

$uploadField->setFolderName('my_folder');

However…how do I access the properties of the related page in this context?

<?php

class BikeImage extends DataObject {

    const TITLE_NAME = 'Title';
    const SORT_ORDER_NAME = 'SortOrder';

    private static $db = array(
        self::TITLE_NAME => 'Varchar',
        self::SORT_ORDER_NAME => 'Int',
    );

    // One-to-one relationship with bike page
    private static $has_one = array(
        'Image' => 'Image',
        'GalleryPage' => 'GalleryPage'
    );

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

        $fields->removeFieldFromTab('Root.Main', 'GalleryPageID');

        $custom_folder_name = 'default';

        // $custom_folder_name = name / id of related gallery page
        // how to access the properties of the related gallery page?

        $uploadField = new UploadField('Image', _t('general.image', 'image'));
        $uploadField->setFolderName($custom_folder_name);
        $fields->addFieldToTab('Root.Main', $uploadField);

        return $fields;
    }

    // Tell the data grid what fields to show in the table
    private static $summary_fields = array(
        'ID' => 'ID',
        self::TITLE_NAME => 'Title'
    );

}

Thx for any pointers!

Avatar
(deleted)

Community Member, 473 Posts

6 July 2013 at 10:35am

$this->GalleryPage() returns the object the has_one relates to.

Avatar
donkikong

Community Member, 3 Posts

11 July 2013 at 4:44am

Edited: 22/07/2013 10:57pm

Thanks…however

$this->GalleryPage()

does return an object, but its ID is zero.

Could it be that the ID is only known after the record is saved?