php - CakePHP 连接查询

标签 php mysql oop cakephp permissions

我正在学习教程 about permissions而不是 CakePHP。 我不明白这个模型(查询)是如何工作的。

任何人都可以向我解释这个查询是如何工作的。我找不到这个变量 hasAndBelongsToMany 到底做了什么。

<?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
                    ),  
                );
}

最佳答案

首先它不是查询。它是模型与其他模型关系的声明。

$hasAndBelongsToMany 意味着你数据库中的每条记录都与另一个表的许多记录相关联,并且其他表中的许多记录也可以与当前记录相关联。

例如,一本书可以有很多作者,一个作者可以有很多书。所以它可以关联为 hasAndBelongsToMany。

在您的情况下,组有很多用户,用户有很多组。权限相同。

关于php - CakePHP 连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5550898/

相关文章:

php - 在我的 PHP 代码中解析错误 : Syntax error, 文件意外结束

php - 更改了 MySQL 的 root 密码,现在 zurmo 无法连接到数据库

php - 在 PHP 中仅将年份转换为日期时间

html - 使用jsp将mysql结果导出为ex​​cel/pdf/etc格式

c# - 流畅的界面设计和代码气味

python - python中如何通过调用__init__方法中的方法来引入对象的属性?

php - 将上传的文件作为电子邮件附件发送

php - 屏蔽状态栏中的 URL

php - PHP 中的 $n preg_replace

php - 可捕获的 fatal error :传递给 Foo::bar() 的参数 1 必须实现接口(interface) BazInterface,给定为空