Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Initial values are empty using DataObjectDecorator

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

Page: 1
Go to End
Author Topic: 1401 Views
  • chrclaus
    Avatar
    Community Member
    29 Posts

    Initial values are empty using DataObjectDecorator Link to this post

    Hi,

    I´m trying to set initial values on a DataObject using a DataObjectDecorator. After creating the owner-object the value is still empty.

    In detail I extended the SiteTree with an custom Object:

    ...
    function extraDBFields() {
    return array(
    'has_one' => array(''MyObject","MyObject");
    );
    }
    ...

    In the same class, I override the method populateDefaults:

    function populateDefaults() {
    $myO = DataObject::get_by_id("MyObject", 1); //this Object with ID=1 exists in db!
    $this->owner->MyObject = $myO;
    }

    After creating a new SiteTree-item the attribute MyObjectID in the db-table SiteTree contains a 0 (zero). I tried

    $item->owner->write();


    in populateDefaults too, but this created a second SiteTree-item without the correct MyObjectID too.

    Without any success in using the DataObjectDecorator I tried another, second solution. I extended CMSMain (in the same way as LeftAndMainDecorator does) and set the attribute on the reference parameter &$item in the function augmentNewSiteTreeItem(&$item). Without success too. The resulting attribute MyObjectID in my SiteTree-item is still 0.

    I verified both method-calls with debug-statements - both extensions itself worked correctly.

    Is there any other initialization after these both extension-calls which overwrites the has_one-relation?

    Best regards,
    chrclaus

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Initial values are empty using DataObjectDecorator Link to this post

    To bypass the relationship setter, try:

    $this->owner->MyObjectID = $myO->ID;

  • chrclaus
    Avatar
    Community Member
    29 Posts

    Re: Initial values are empty using DataObjectDecorator Link to this post

    Hi Hamish,

    thanks for your tip, but this did not change the initial value too. I tried to change the default page-title with

    $item->Title = "chrclaus";


    in function augmentNewSiteTreeItem of the extension or

    $this->owner->Title = "chrclaus";


    in the function populateDefaults.
    But after clicking in CMSMain "Create page"->"Go" the initial values at the right side of CMSMain are still Title="NewPage" and MyObjectID=0.

    Is it possible, that these initial values are overwritten after the extension had been called? I tried both solutions in v2.3.2 and 2.3.3

    Best regards,
    chrclaus

    Correction:
    Setting the title works. I mixed up my sources between 2.3.2 and 2.3.3 and changed the wrong file But setting MyObject still doesn´t work!

  • chrclaus
    Avatar
    Community Member
    29 Posts

    Re: Initial values are empty using DataObjectDecorator Link to this post

    I found a workaround and probably an issue. The value MyObjID was not written to the database-manipulation information. I took a look in this complex array and did not find the entry for MyObjID with the current ID I had set in my decorator.

    Therefore I implemented another extension-method in my decorator:

    function augmentWrite(&$manipulation) {
    $manipulation['SiteTree']['fields']['MyObjID'] = 8;
    }


    After creating a new page, the initial value is correct.

    Best regards,
    chrclaus

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