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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Using the Intelligent Constructor for more than two data objects


Go to End


5 Posts   1487 Views

Avatar
Samba Sam

Community Member, 85 Posts

8 December 2009 at 7:24am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 7:46am

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. :)

Avatar
Samba Sam

Community Member, 85 Posts

8 December 2009 at 8:48am

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

Avatar
Samba Sam

Community Member, 85 Posts

8 December 2009 at 12:52pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 December 2009 at 1:10pm

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