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.

Archive /

Our old forums are still available as a read-only archive.

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

Using ->customise on DataObjectSet?


Reply


5 Posts   2841 Views

Avatar
banal

Community Member, 901 Posts

25 February 2008 at 10:19pm

Hello

I'm trying to add some custom Fields to a Output generated by the Controller. I tried something like this:

$set = DataObject::get( ... );
return $set->customise( array( 'CustomField' => $myValue ));

This doesn't seem to work though. If i do that, it doesn't preserve the values i got from the DB via DataObject::get().
If i do a foreach loop over the Set and customize every DataObject, it doesn't append the new values:

$set = DataObject::get( ... );
foreach($set as $item){
$item = $item->customise(array('CustomField' => $myValue));
}
return $set;

What is the right syntax to customise every DataObject in a DataObjectSet?
Input is much appreciated. Thanks

Avatar
blueskies

Community Member, 42 Posts

3 March 2008 at 3:27am

I'm trying to do the exact same thing and can't get it working either!

Just posting to bump the topic and subscribe to the replies ;-)

Avatar
dio5

Community Member, 501 Posts

3 March 2008 at 5:30am

Edited: 03/03/2008 5:33am

I think it might have to be more like this in your foreach loop:

$item = $item->First()->customise(array('CustomField' => $myValue ));

However, I could be wrong though, you could also create a new set to output.

I've only used it in this context so far:

$object= DataObject::get_by_id("Type", $SomeID);
$data = array("Object" => $object);
         
return $this->customise($data)->renderWith(array("MyPage", "Page"));

Avatar
blueskies

Community Member, 42 Posts

3 March 2008 at 10:01am

@dio5

Thanks for the reply, but doesn't work for me. I get a:
Fatal error: Call to a member function customise() on a non-object

My current example:

$CompanyPageHolder = DataObject::get_one("CompanyPageHolder");
$CompanyPageSet   = DataObject::get("CompanyPage", "ShowInMenus = 1 AND parentID = $CompanyPageHolder->ID","Sort");
      
$CustomCompanyPageSet = new DataObjectSet(); //Result dataset
foreach ($CompanyPageSet as $CompanyPageObject) {
   $item = $CompanyPageObject->First()->customise(array('CustomField' => 'Testgin' ));
   $CustomCompanyPageSet ->merge($item);
}
      
return $CustomCompanyPageSet;

This is similar to the example on:http://doc.silverstripe.com/doku.php?id=tutorial:3-forms for the function BrowserPollResults(). The problem with the example is that it pushes array data into the new set. If I do that, then I have to convert CompanyPageSet to an array and I lose my image data (thumbnail and other company page images) - and only the imageID remains.

Basically I'm looking for a working example of the customize functionality andat the same time keep the dataobject info....

Avatar
dio5

Community Member, 501 Posts

3 March 2008 at 12:02pm

Edited: 03/03/2008 12:04pm

Sorry, my advice for using First() wasn't completely correct I believe and more applicable when you loop through an array-result of groupBy();

What happens when you do:

$CustomCompanyPageSet = new DataObjectSet(); //Result dataset

foreach ($CompanyPageSet as $CompanyPageObject) {

   $item = $CompanyPageObject->customise(array('CustomField' => 'Testgin' ));

   $CustomCompanyPageSet ->push($item);

}

That doesn't work? (I haven't done a customise like this to be honest, and maybe it won't work like this, ssadmins?)

You could always still use the arraydata method (like in the tutorial) and pass your object as well as yr new field.
      
Think that would become something like (in the foreach loop):

$data = array(
      "CustomField" => "Testing",
      "CompanyPageObject" => $CompanyPageObject
               );   
$CustomCompanyPageSet->push(new ArrayData($data));   

Of course, then it's not really available in the normal way.