0

Tables

User(id)
Profile(id, *user_id*, type)
Attribute(id, *attribute_name_id*, value)
AttributeName(id, name)
ProfileAttribute(id, *profile_id*, *attribute_id*)

Relationships

The relationships are set up correctly (and go both ways, hasMany/belongsTo).

User hasMany Profile
Profile hasMany ProfileAttribute
Attribute hasMany ProfileAttribute
  (could be written Profile hasMany Attribute through ProfileAttribute)
AttributeName hasMany Attribute

Goal

For a specified User id, with a find() in the User model, I only want the following fields, laid out as such:

$results[Profile.type][AttributeName.name][Attribute.value]

Is it even possible to retrieve results arranged like this? I've been playing around with Find and Containable for hours, but, first time trying to do anything complicated like this with Cake, I can't get the hang of it.

Thanks!

EDIT

I'm getting these results now, all that I need, but nowhere near the desired format above -can it be done as part of the find, or does it need to be sorted after?

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50

1 Answers1

1

Yep, it's possible. You just have to specify fields on containable:

$this->User->find('all', array(
    'conditions' => array('User.id' => $id),
    'fields' => array('id'),
    'contain' => array(
        'Profile' => array(
            'fields' => array('id','type'),
            'ProfileAttribute' => array(
                'fields' => array('id'),
                'AttributeName' => array(
                    'fields' => array('id','name'),
                    'Attribute' => array(
                        'fields' => array('id','value')
                    )
                )
            )
        )
    )
);

Be wary that when you use contain and fields options, you have to specify the id so it can make the association (check the docs)

EDIT: I don't know if you can group contained data as the docs didn't say anything about that, but probably you can, as they accept some parameters as in the main query. You can try it, adding group to any contained data that you want to group

Roberto Maldonado
  • 1,585
  • 14
  • 27
  • Swapping AttributeName and Attribute above gets me all the data I need - removing id doesn't seem to matter, cake includes it anyway. However, the structure returned is quite a mess with several useless layers - I'll add it to the question. Is there a way to change this, or does it have to be reorganised or used as is after find? – StringsOnFire Feb 06 '14 at 20:29
  • Don't know if you can retreive an empty array from the models you don't want data, but if not, force it to retreive just the id adding a `fields` option. Better one field than all of them :P – Roberto Maldonado Feb 06 '14 at 20:46
  • That works, thanks. I don't think there is a way to reduce the data other than that, as it'll always return the keys. Thanks! – StringsOnFire Feb 06 '14 at 20:53
  • If you want data in a prettier format, maybe you could take a loot at `Set` utility class for array management: http://book.cakephp.org/2.0/en/core-utility-libraries/set.html – Roberto Maldonado Feb 06 '14 at 20:55
  • I'm looking into Hash now, almost got it as I want it – StringsOnFire Feb 06 '14 at 21:02