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 /

Initial values are empty using DataObjectDecorator


Reply


4 Posts   1559 Views

Avatar
chrclaus

Community Member, 29 Posts

10 August 2009 at 7:32pm

Edited: 10/08/2009 7:35pm

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

Avatar
Hamish

Community Member, 712 Posts

11 August 2009 at 6:34pm

To bypass the relationship setter, try:

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

Avatar
chrclaus

Community Member, 29 Posts

12 August 2009 at 7:57am

Edited: 12/08/2009 6:30pm

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!

Avatar
chrclaus

Community Member, 29 Posts

15 August 2009 at 7:25am

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