Coder Social home page Coder Social logo

Comments (8)

colymba avatar colymba commented on July 30, 2024

onBeforeSerialize would be the right place for those kind of cases. If you have it defined on a DataExtension, you should have something like that:

public function onBeforeSerialize()
{
    $this->owner->FullName = $this->owner->fullname();
}

that will create a property FullName on your dataobject that will be serialized with the rest as the DB fields like normal.

from silverstripe-restfulapi.

mirkonz avatar mirkonz commented on July 30, 2024

Awesome, thanks. I will give it a go tonight.

from silverstripe-restfulapi.

mirkonz avatar mirkonz commented on July 30, 2024

Hmm, I'm still struggling to get this to work.

class Student extends DataObject {
  private static $db = array(
    'firstname' => 'Varchar',
    'lastname' => 'Varchar'
  );
  public static $allowed_actions = array(
    'fullname'
  );
  public function onBeforeSerialize() {
    // This works:
    // Debug::show($this->owner-> fullname());

    // This doesn't work:
    $this->owner-> FullName = $this->owner-> fullname();
    // unset($this->owner->firstname);
  }
}
class StudentExtension extends DataExtension {
  public function fullname() {
    return $this->firstname . ' ' . $this->lastname;
  }
}

_config.php
Edited:

Student::add_extension('StudentExtension');

I also tried to put the onBeforeSerialize method on the DataExtension.

Is there more configuration required in the config.yml file?

from silverstripe-restfulapi.

colymba avatar colymba commented on July 30, 2024

$this->owner only work if you are in the DataExtension.
Then your config should read: Student::add_extension('StudentExtension');

So with this, you should have:

class Student extends DataObject {
  private static $db = array(
    'firstname' => 'Varchar',
    'lastname' => 'Varchar'
  }
  public static $allowed_actions = array(
    'fullname'
  ); 
  public function fullname() {
    return $this->firstname . ' ' . $this->lastname;
  }
}

class StudentExtension extends DataExtension { 
 public function onBeforeSerialize() {   
    $this->owner->FullName = $this->owner-> fullname();
    // unset($this->owner->firstname);
  }
}

from silverstripe-restfulapi.

mirkonz avatar mirkonz commented on July 30, 2024

That's not working for me.
This is how my response looks like:
{"students":[{"id":1,"firstname":"John","lastname":"Doe"}]}

Sorry, I made a mistake in my previews comment. I actually had Student::add_extension('StudentExtension') in my config, as well as $this->owner

from silverstripe-restfulapi.

colymba avatar colymba commented on July 30, 2024

So sorry, that's my bad. This used to work in earlier releases but now the serializer uses the Config api, so updating properties on the DataObject wont work that simply. You would have to update the static $db:

class StudentExtension extends DataExtension { 
 public function onBeforeSerialize() { 
    $db = $this->owner->config()->get('db');
    $db['fullName'] = '';
    $this->owner->config()->update('db', $db);  
    $this->owner->setField('fullName', $this->owner->fullname());
  }
}

Which I'll admit feels very awkward... and I can't really test this right now...

Best option might be the onAfterSerialize which works something like:

 public function onAfterSerialize(&$formattedDataObjectMap) { 
    $formattedDataObjectMap['fullName'] = $this->owner->fullname();
  }

from silverstripe-restfulapi.

mirkonz avatar mirkonz commented on July 30, 2024

That works perfectly!
Thank you so much for your time.

from silverstripe-restfulapi.

colymba avatar colymba commented on July 30, 2024

Tracking doc updates here #27

from silverstripe-restfulapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.