Jump to:

5521 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SS3 Get related has_one object in data object

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

Page: 1
Go to End
Author Topic: 634 Views
  • donkikong
    Avatar
    Community Member
    2 Posts

    SS3 Get related has_one object in data object Link to this post

    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!

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: SS3 Get related has_one object in data object Link to this post

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

  • donkikong
    Avatar
    Community Member
    2 Posts

    Re: SS3 Get related has_one object in data object Link to this post

    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?

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