Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Moving data to a DataObject

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

Page: 1
Go to End
Author Topic: 464 Views
  • mv
    Avatar
    Community Member
    5 Posts

    Moving data to a DataObject Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Moving data to a DataObject Link to this post

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

  • mv
    Avatar
    Community Member
    5 Posts

    Re: Moving data to a DataObject Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Moving data to a DataObject Link to this post

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

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