7912 Posts in 1355 Topics by 930 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 887 Views |
-
Using the Intelligent Constructor for more than two data objects

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 -
Re: Using the Intelligent Constructor for more than two data objects

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.
-
Re: Using the Intelligent Constructor for more than two data objects

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 -
Re: Using the Intelligent Constructor for more than two data objects

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
-
Re: Using the Intelligent Constructor for more than two data objects

8 December 2009 at 1:10pm
Yup, that's the way you do it. The intelligent constructor is the exception, not the rule.
| 887 Views | ||
|
Page:
1
|
Go to Top |

