17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2511 Views |
-
Using ->customise on DataObjectSet?

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 -
Re: Using ->customise on DataObjectSet?

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 ;-)
-
Re: Using ->customise on DataObjectSet?

3 March 2008 at 5:30am Last edited: 3 March 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")); -
Re: Using ->customise on DataObjectSet?

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-objectMy 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....
-
Re: Using ->customise on DataObjectSet?

3 March 2008 at 12:02pm Last edited: 3 March 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.
| 2511 Views | ||
|
Page:
1
|
Go to Top |



