0

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

关系

关系设置正确(并且双向,hasMany/belongsTo)。

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

目标

对于指定的用户 ID,在用户模型中有一个 find(),我只想要以下字段,如下所示:

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

甚至可以检索这样排列的结果吗?我一直在玩 Find and Containable 几个小时,但是,第一次尝试用 Cake 做这样复杂的事情,我无法掌握它的窍门。

谢谢!

编辑

我现在得到了这些结果,这是我所需要的,但与上面所需的格式相去甚远——它可以作为查找的一部分完成,还是需要在之后进行排序?

4

1 回答 1

1

是的,有可能。您只需要在可包含的字段上指定:

$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')
                    )
                )
            )
        )
    )
);

请注意,当您使用containfields选项时,您必须指定,id以便它可以建立关联(查看文档)

编辑:我不知道您是否可以对包含的数据进行分组,因为文档没有说明任何内容,但可能您可以,因为它们接受主查询中的一些参数。您可以尝试一下,添加group到您想要分组的任何包含的数据中

于 2014-02-06T19:19:35.357 回答