Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Using ->customise on DataObjectSet?

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

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

Page: 1
Go to End
Author Topic: 2766 Views
  • banal
    Avatar
    Community Member
    901 Posts

    Using ->customise on DataObjectSet? Link to this post

    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

  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: Using ->customise on DataObjectSet? Link to this post

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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Using ->customise on DataObjectSet? Link to this post

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

  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: Using ->customise on DataObjectSet? Link to this post

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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Using ->customise on DataObjectSet? Link to this post

    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.

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