👍 (1)

Funnily enough, exactly what I'm working on now:

  1. public function getStaff() {
  2. $staff = StaffMember::get()->filter(['ShowOnHomepage' => 1])->limit(22)->sort('RAND()');
  3. $people = [];
  4. foreach ($staff as $person) {
  5. $people[] = [
  6. 'Name' => $person->Title,
  7. 'Image' => ($person->ImageID > 0) ? $person->Image()->Diamond()->ScaleWidth(180)->URL : null
  8. ];
  9. }
  10. return json_encode($people);
  11. }

Quite often, when I do this kind of thing, I need to apply a bit of logic to the results, so just end up iterating the DataList and building a lightweight array which is then encoded for output.

I'm just wondering about the neatest way to do it.

DataObject::get() will return a DataList, so you probably don't want to just directly try and json_encode that anyway

The ORM will lazy-load, so it won't run the queries until it actually knows what you want.

Since the Security Admin already exists, you'll probably need to customise that instead. What are you trying to do?

SecurityAdmin is a bit different to a normal ModelAdmin

Are the google scripts loading? Anything in the console?