Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Using the Intelligent Constructor for more than two data objects

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1004 Views
  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Using the Intelligent Constructor for more than two data objects Link to this post

    Hi,
    I suspect I am approaching this wrongly. I've created two DataObject files, one for the left column images in my template and the other for right column, which are editable by "Left" and "Right" tabs in the CMS.

    The other data object file is the same but defines the class ImageLeft.
    <?php
    class ImageRight extends DataObject
    {
    static $db = array (
    'CaptionRight' => 'Varchar(50)'
    );

    static $has_one = array (
    'Page' => 'Page',
    'ImageRight' => 'Image'
    );

    function getCMSFields()
    {
    return new FieldSet(
    new ImageField('ImageRight'),
    new TextField('CaptionRight','Caption')
    );
    }
    }
    ?>

    The relevant code from Page.php looks like:
    static $has_many = array (
    'ImagesLeft' => 'ImageLeft',
    'ImagesRight' => 'ImageRight'
    );

    function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->addFieldToTab("Root.Content.Left", new ImageDataObjectManager($this));
    $fields->addFieldToTab("Root.Content.Right", new ImageDataObjectManager($this));
    return $fields;

    It works fine if I just use the intelligent constructor once (e.g., Left tab). But when I add the second for the right column it just pulls the ImageLeft data object variables.

    Thanks for the help,
    Sam

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using the Intelligent Constructor for more than two data objects Link to this post

    Sorry, that's not what the intelligent constructor does. It only works if you have only one has_many relationship.

    It's an intelligent constructor. Not a psychic constructor.

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Using the Intelligent Constructor for more than two data objects Link to this post

    Hi,
    How would I use the full constructor(s) with more than one has_many relationship, as I am trying to do above?
    Thanks,
    Sam

  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Using the Intelligent Constructor for more than two data objects Link to this post

    So, I got it to work by doing the following in my Page.php file:
    static $has_many = array (
    'ImagesRight' => 'ImageRight',
    'ImagesLeft' => 'ImageLeft'
    );

    function getCMSFields() {
       $fields = parent::getCMSFields();
       $manager_right = new ImageDataObjectManager(
    $this,
           'ImagesRight',
           'ImageRight',
           'ImageRight',
           array('CaptionRight' => 'Caption'),
           'getCMSFields');
       $manager_left = new ImageDataObjectManager(
    $this,
           'ImagesLeft',
           'ImageLeft',
           'ImageLeft',
           array('CaptionLeft' => 'Caption'),
           'getCMSFields');
    $fields->addFieldToTab("Root.Content.Left",$manager_left);
    $fields->addFieldToTab("Root.Content.Right",$manager_right);
    return $fields;

    If anyone knows a better way of doing this, please share.

    Sam

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using the Intelligent Constructor for more than two data objects Link to this post

    Yup, that's the way you do it. The intelligent constructor is the exception, not the rule.

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