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.

Data Model Questions

Moving data to a DataObject


Reply

4 Posts   527 Views

Avatar
mv

13 July 2011 at 10:17pm Community Member, 5 Posts

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

13 July 2011 at 11:02pm Forum Moderator, 1796 Posts

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

13 July 2011 at 11:39pm (Last edited: 13 July 2011 11:40pm), Community Member, 5 Posts

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

13 July 2011 at 11:42pm Forum Moderator, 1796 Posts

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);