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.

Data Model Questions /

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

Moving data to a DataObject


Go to End


4 Posts   1278 Views

Avatar
mv

Community Member, 5 Posts

13 July 2011 at 10:17pm

Hello there!

After a while of playing with your CMS I hit some problems. I'm trying to allow editing data stored in a separate dataobject through a certain page in the administration panel. Here's the code I've written:
OtherPage

<?php
class OtherPage extends Page {
   static $db = array(
   );
   
   static $has_one = array(
		'SomeData1' => 'OtherData',
		'SomeData2' => 'OtherData',
		'SomeData3' => 'OtherData',
   );
   
    function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab('Root.Content.Something', new TextareaField('SomeData1', '...'));
		$fields->addFieldToTab('Root.Content.Something', new TextareaField('SomeData2', '...'));
		$fields->addFieldToTab('Root.Content.Something', new TextareaField('SomeData3', '...'));
         
		return $fields;
    }
	
	static $allowed_children = 'none';
}
 
class OtherPage_Controller extends Page_Controller {
}

OtherData
<?php
 
class OtherData extends DataObject {
    static $db = array(
		'SomeData1' => 'Text',
		'SomeData2' => 'Text',
		'SomeData3' => 'Text',
    );
	
    static $has_one = array(
        'OtherPage' => 'OtherPage'
    );
}

I believe there is missing some link that would connect the fields in OtherPage with corresponding fields in database managed by OtherData. The fields' content is persistently empty, even though I'm populating them with text. What am I doing wrong?

Thanks in advance.

Avatar
swaiba

Forum Moderator, 1899 Posts

13 July 2011 at 11:02pm

Hi mv,

Welcome to silverstripe!

instead of...

$fields->addFieldToTab('Root.Content.Something', new TextareaField('SomeData1', '...')); 

try...

$fields->addFieldToTab("Root.SomeData1",new HasOneComplexTableField($this,'SomeData1','OtherData',null,null,'ID='.$this->SomeData1ID));

Avatar
mv

Community Member, 5 Posts

13 July 2011 at 11:39pm

Edited: 13/07/2011 11:40pm

Hello swaiba, thanks for your help.

Your solution kind of works, however it now allows me to add more than one entry for every field eg. SomeData1. What if I want to keep it simple and display only a one field for a single value (like TextareaField does), that would be saved in OtherData?

Now that I thought about that I came to a conclusion that I might be confusing the idea of DataObject. Maybe I shouldn't store single values in such objects and move them to the *Page classes instead? Previously I thought it was wrong, hence this topic.

Avatar
swaiba

Forum Moderator, 1899 Posts

13 July 2011 at 11:42pm

yes, keeping in the Page object is much better - that is what I would do....

re the HasOne...

you could do this...

$hasOneCTF = new HasOneComplexTableField($this,'SomeData1','OtherData',null,null,'ID='.$this->SomeData1ID);
$hasOneCTF->setPermissions(array('edit','show'));
$hasOneCTF->Markable = false;
$fields->addFieldToTab("Root.SomeData1",hasOneCTF);