LordZardeck
LordZardeck

Reputation: 8283

format cakephp find result

If I do a mainModel->find('all') call on a model, my data is returned in this format:

[
    {
        'mainModel': {},
        'associatedModel1': {},
        'associatedModel2': {},
        'associatedModel3': {},
        'associatedModel4': {}
    },
    {
        'mainModel': {},
        'associatedModel1': {},
        'associatedModel2': {},
        'associatedModel3': {},
        'associatedModel4': {}
    }
]

how can i get it to return in this format:

[
    {
        'mainModel': {
            'associatedModel1': {},
            'associatedModel2': {},
            'associatedModel3': {},
            'associatedModel4': {}
        }
    },
    {
        'mainModel': {
            'associatedModel1': {},
            'associatedModel2': {},
            'associatedModel3': {},
            'associatedModel4': {}
        }
    }
]

please excuse my object-notation formatting. I find JSON to be the prettiest way to explain data structures.

Upvotes: 1

Views: 483

Answers (1)

RichardAtHome
RichardAtHome

Reputation: 4313

You can't alter the format that data is returned from in a find, but you CAN mangle the format after it's been returned. Check out the Set library: http://book.cakephp.org/2.0/en/core-utility-libraries/set.html

The burning question is, WHY do you want to change the structure? CakePHP expects data in this structure and if you start changing it, your app will break in unexpected/hard to track down ways...

Upvotes: 1

Related Questions