0

我有三个模型:User Image Group

  • 用户属于一个图像;
  • 组属于用户(管理员)
  • Group 拥有并属于用户;
  • 用户拥有并属于群组;
  • 用户和组模型与users_groups表连接;

当我查看一个组时,列出了成员(用户,这可行)我需要提取他们的图像我应该怎么做?

4

1 回答 1

0

您应该使用ContainableBehavior。它将允许您控制您提取的数据。

首先,将其添加到您的 AppModel。我也总是设置$recursive = -1,所以永远不会提取额外的数据。对依赖$recursive数据的现有应用程序执行此操作时要小心。

class AppModel extends Model {
    public $recursive = -1;
    public $actsAs = array('Containable');
}

然后,在查找调用期间,使用“包含”查询键。例如,在您的 GroupsController 上:

public function view($id = null) {
  $results = $this->Group->find('first', array(
    'conditions' => array(
        'Group.id' => $id
    ),
    'contain' => array(
        'User' => array(
            'Image'
        )
    )
  ));
}

This will bring in the User information associated with the group, along with the Image information associated with the users.

于 2012-06-18T14:35:54.373 回答