简体   繁体   中英

CakePHP JOINS Query

I was following a tutorial about permissions instead CakePHP. I don't understand how this model (query) works.

Can anybody explain me how this query works.. I can't find what this variable hasAndBelongsToMany exactly does.

<?php
class Group extends Appmodel {
    var $name = 'Group';
    var $useTable 'groups';
    var $hasAndBelongsToMany = array(
    'Permission' => array('className' => 'Permission',
                    'joinTable' => 'groups_permissions',
                    'foreignKey' => 'group_id',
                    'associationForeignKey' => 'permission_id',
                    'unique' => true
                    )
    'User' => array('className' => 'User',
                    'joinTable' => 'groups_users',
                    'foreignKey' => 'group_id',
                    'associationForeignKey' => 'user_id',
                    'unique' => true
                    ),  
                );
}

First of all it is not a query. it is a declaration of relation of the model to other models.

$hasAndBelongsToMany means that every record in your database is associated with many records of another table, and the many records from other table can be associated with the current record as well.

for example Book can have many authors, and an authors can have many books. so it can be related as hasAndBelongsToMany.

in your case Group has many users, and users have many groups. same for permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM