Derived attributes in recorset

Hi!

I need to output a recordset array as I’m working on a REST model. I currently have

[indent]Menu:

[indent]- ‘name’ => name

  • ‘icon’ => icon

  • ‘url’ => url[/indent][/indent]

But, to follow the whole site data structure I’d need:

[indent]Menu:

[indent]- ‘name’ => name

  • ‘icon’ => icon

  • ‘apiCall’:

    [indent]- ‘url’ => url

    • ‘method’ => ‘GET’[/indent][/indent][/indent]

I’m doing:


$menus = Menu::model()->findAll();

But I don’t know how to get the recordset as I need or how to transform it. I’ve tried a foreach but I get ‘Property “Menu.apiCall” is read only.’

Could anyone point me in the right direction?

Thanks!

You just showed us to few code lines / details. So I’m just guessing.

First of all, can you do two iterations, one inside another one. Just fire another iteration on ‘apiCall’ to make you second level structure, just as you built first level structure (i.e. the one that holds ‘apiCall’).

Second thing. AFAIK, the fact that some property is read only does not stops you from using it in foreach. Foreach (as name says) is a simple iteration loop that reads value, not modify it. Therefore it should handle read only attributes just as it do with read-write ones. Are you 100% sure that here lies the problem?

Cheers,

Trejder

Well, the problem was really easy to solve. I was making the same mistake over and over. All I had to do was


foreach ($menus as $menu) {

    $m = $menu->attributes;

    ...

}



I was trying to access $menu directly and hence the error, now that I’ve found my error it’s done in a moment.

Thanks Trejder