我有3张桌子。软件,权限和中心。我可以在软件和权限之间以及中心和权限表之间建立关联。我想获得特定中心的软件。例如,当我访问/centres/:id/softwares.json
时,将返回如下格式。
"Centre": {
"id": "1",
"name": "centre1",
"alias": "CN1"
"Software": [
{
"id": "1",
"name": "software1",
"description": "description1"
},
{
"id": "2",
"name": "software2",
"description": "description2"
},
...
]
}
public $hasMany = 'Permission';
public $hasAndBelongsToMany = array(
'Software' => array(
'className' => 'Software',
'joinTable' => 'permissions',
'foreignKey' => 'id',
'associationForeignKey' => 'software_id',
'unique' => true,
)
);
这是检索特定中心可用软件的示例SQL(id:1)
SELECT s.* FROM softwares s JOIN permissions p ON p.software_id = s.id WHERE p.centre_id=1;