【问题标题】:Defining Model Relations for ACL Interface (gui) with CakePHP使用 CakePHP 为 ACL 接口 (gui) 定义模型关系
【发布时间】:2009-12-09 09:08:17
【问题描述】:

我已经在我的 cakePHP 应用上成功设置了 Auth 和 ACL。

如何,现在我想建立一个界面来管理 ACL、ARO 和 ACO

ARO 和 ACO 使用树行为非常容易构建。 ACL 怎么让我有点搞砸了……尤其是在定义模型关系时。

我已将我的 aco 和 aro 模型分别命名为 Myaco 和 Myaro,因为 Aco 和 Aro 已经被 AclComponent 占用。我的 ACL 模型是 Myacl。

Myaco 是一个“独立”对象,它与任何其他模型无关。 Myaro "belongsTo" 'Customer' 模型(这是我的“user”模型)并定义如下:

//Myaro model
var $belongsTo = array('Customer'=> array(
                                        'foreignKey'    => 'foreign_key'
                                        ));

现在我理解的 Myacl 模型是 $hasAndBelongsToMany -> Myaco 和 Myaro,因为它是一个连接其他两个模型的表; 因此,我已将其设置为:

class Myacl extends AppModel {
    var $useTable = 'aros_acos';
    var $hasAndBelongsToMany = array(
        'Myaro' => array(
                        'foreignKey' => 'aro_id',
                        'associationForeignKey' => 'id',
                        'joinTable' => 'aros_acos'
                    ),
        'Myaco' => array(
                        'foreignKey' => 'aco_id',
                        'associationForeignKey' => 'id',
                        'joinTable' => 'aros_acos'
                        ),
    );
}

现在要为 ACL 构建主“界面”,我想“查找('all')”ACL 记录,并将链接(递归)到“客户”级别 - 这样我就可以显示来自aro 表的组和实际客户;所以我将 Myacl 模型的 recursive 参数设置为“2”

虽然从调试窗格的 SQL 查询来看 - 关系根本不正确.. 我设置关系的方式完全错了吗?

以下是 find('all') 调用中的基本 SQL 查询:

从此代码(在 Myacl 控制器中):

$this->Myacl->recursive = 2;
$allRules = $this->Myacl->findAll();

查询是:

1   DESCRIBE `aros`     7   7   13
2   DESCRIBE `acos`     7   7   1
3   DESCRIBE `aros_acos`        3   3   1
4   DESCRIBE `customers`        9   9   2
5   SELECT `Myacl`.`id`, `Myacl`.`aro_id`, `Myacl`.`aco_id` FROM `aros_acos` AS `Myacl` WHERE 1 = 1 ORDER BY `Myacl`.`id` ASC       9   9   0
6   SELECT `Myaro`.`id`, `Myaro`.`parent_id`, `Myaro`.`model`, `Myaro`.`foreign_key`, `Myaro`.`alias`, `Myaro`.`lft`, `Myaro`.`rght`, `ArosAco`.`id`, `ArosAco`.`aro_id`, `ArosAco`.`aco_id` FROM `aros` AS `Myaro` JOIN `aros_acos` AS `ArosAco` ON (`ArosAco`.`aro_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9) AND `ArosAco`.`id` = `Myaro`.`id`) WHERE 1 = 1      9   9   0
7   SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
8   SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
9   SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
10  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
11  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
12  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
13  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
14  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
15  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
16  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
17  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
18  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
19  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
20  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
21  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
22  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
23  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
24  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
25  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
26  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
27  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
28  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
29  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
30  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
31  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 1        1   1   0
32  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 2        1   1   0
33  SELECT `Customer`.`id`, `Customer`.`username`, `Customer`.`email`, `Customer`.`password`, `Customer`.`company_name`, `Customer`.`contact_person`, `Customer`.`contact_email`, `Customer`.`contact_phone`, `Customer`.`disabled` FROM `customers` AS `Customer` WHERE `Customer`.`id` = 4        1   1   0
34  SELECT `Myaco`.`id`, `Myaco`.`parent_id`, `Myaco`.`model`, `Myaco`.`foreign_key`, `Myaco`.`alias`, `Myaco`.`lft`, `Myaco`.`rght`, `ArosAco`.`id`, `ArosAco`.`aro_id`, `ArosAco`.`aco_id` FROM `acos` AS `Myaco` JOIN `aros_acos` AS `ArosAco` ON (`ArosAco`.`aco_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9) AND `ArosAco`.`id` = `Myaco`.`id`) WHERE 1 = 1

您可以从“客户”查询的数量和连接中看到 - 模型关系严重“偏离”了一些东西..

非常感谢任何建议或线索。 谢谢, 肯。

【问题讨论】:

    标签: cakephp associations orm modeling acl


    【解决方案1】:

    我刚从 ACL 开始,但我认为您在 acl 关系中弄错了。 aro hasAndBelongsToMany aco,使 acl 成为它们之间的存根。所以,如果你正在制作一个acl的模型,它应该有一个belongsTo aco和belongsTo aco。 Aro 和 aco 应该有一个 hasMany acl。 希望对你有帮助;D

    【讨论】:

    • @Ferco:感谢您的回复,有点晚了,但我已经想通了。我按照你的建议做了 - 相同的关系。谢谢!
    猜你喜欢
    • 2021-07-30
    • 2014-02-07
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多