CakePHP 2:Acl教程不起作用

时间:2012-11-13 19:42:52

标签: cakephp foreign-keys acl cakephp-2.2

正如标题所说,我对CakePHP 2的Acl教程有疑问。 我完全按照教程中提到的方式完成了所有工作,但它仍然不起作用。

我知道互联网上有很多人也有本教程的问题,但我找不到任何博客条目或其他像我一样的问题。 当我尝试访问我的应用程序的方向时,我收到以下错误:

Notice (8): Undefined index: group_id [APP\Model\User.php, line 98]

AclNode::node() - Couldn't find Aro node identified by "Array ( [Aro0.model] => Group [Aro0.foreign_key] => ) "

因此,传递给Acl的foreign_key根本没有任何值。 但奇怪的是 - 至少在用户控制器的功能中 - foreign_key设置正确但是当它应该传递给Acl时它已经消失了

Nr  Query   Error   Affected    Num. rows   Took (ms)

1   SELECT COUNT(*) AS `count` FROM `cake`.`users` AS `User` WHERE `User`.`id` = 4
2   SELECT COUNT(*) AS `count` FROM `cake`.`users` AS `User` WHERE `User`.`id` = 4
3   SELECT COUNT(*) AS `count` FROM `cake`.`users` AS `User` WHERE `User`.`id` = 4
4   UPDATE `cake`.`users` SET `modified` = '2012-11-13 19:59:47' WHERE `cake`.`users`.`id` = '4'
5   SELECT `User`.`group_id` FROM `cake`.`users` AS `User` WHERE `User`.`id` = 4 LIMIT 1
6   SELECT `Aro`.`id`, `Aro`.`parent_id`, `Aro`.`model`, `Aro`.`foreign_key`, `Aro`.`alias` FROM `cake`.`aros` AS `Aro` LEFT JOIN `cake`.`aros` AS `Aro0` ON (`Aro`.`lft` <= `Aro0`.`lft` AND `Aro`.`rght` >= `Aro0`.`rght`) WHERE `Aro0`.`model` = 'Group' AND `Aro0`.`foreign_key` = 2 ORDER BY `Aro`.`lft` DESC
7   SELECT `Aro`.`id`, `Aro`.`parent_id`, `Aro`.`model`, `Aro`.`foreign_key`, `Aro`.`alias` FROM `cake`.`aros` AS `Aro` LEFT JOIN `cake`.`aros` AS `Aro0` ON (`Aro`.`lft` <= `Aro0`.`lft` AND `Aro`.`rght` >= `Aro0`.`rght`) WHERE `Aro0`.`model` = 'User' AND `Aro0`.`foreign_key` = 4 ORDER BY `Aro`.`lft` DESC
8   SELECT COUNT(*) AS `count` FROM `cake`.`aros` AS `Aro` WHERE `Aro`.`id` = 6
9   SELECT COUNT(*) AS `count` FROM `cake`.`aros` AS `Aro` WHERE `Aro`.`id` = 6
10  SELECT `Aro`.`parent_id` FROM `cake`.`aros` AS `Aro` WHERE `Aro`.`id` = 6 LIMIT 1
11  SELECT `Aro`.`id`, `Aro`.`parent_id`, `Aro`.`lft`, `Aro`.`rght` FROM `cake`.`aros` AS `Aro` WHERE 1 = 1 AND `Aro`.`id` = 6 LIMIT 1
12  SELECT `Aro`.`id`, `Aro`.`lft`, `Aro`.`rght` FROM `cake`.`aros` AS `Aro` WHERE 1 = 1 AND `Aro`.`id` = 2 LIMIT 1
13  SELECT COUNT(*) AS `count` FROM `cake`.`aros` AS `Aro` WHERE `Aro`.`id` = 6
14  UPDATE `cake`.`aros` SET `parent_id` = 2, `model` = 'User', `foreign_key` = 4, `id` = 6 WHERE `cake`.`aros`.`id` = '6'
15  SELECT `Aro`.`id`, `Aro`.`parent_id`, `Aro`.`model`, `Aro`.`foreign_key`, `Aro`.`alias` FROM `cake`.`aros` AS `Aro` LEFT JOIN `cake`.`aros` AS `Aro0` ON (`Aro`.`lft` <= `Aro0`.`lft` AND `Aro`.`rght` >= `Aro0`.`rght`) WHERE `Aro0`.`model` = 'Group' AND `Aro0`.`foreign_key` IS NULL ORDER BY `Aro`.`lft` DESC

错误消息中的行是:

public function bindNode($user) {
    return array('model'=>'Group','foreign_key'=>$user['User']['group_id']);
}

那么为什么foreign_key的值首先是“2”(它应该如何)以及之后的NULL?

我希望你们中的一些人能够帮助我。 感谢。

PS:对于那些更喜欢我的问题的德语版本的人,here它是

更新 我能够通过以下方式使其适用于用户控制器的所有功能:

public function bindNode() {
    $this->id;
    $data = $this->read();
    return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
    }

但对于所有其他控制器,它仍然是相同的.. 有没有人知道如何在模型中获取用户数据,以便它也适用于其他控制器?

1 个答案:

答案 0 :(得分:4)

我知道回答我自己的问题有点愚蠢,但我没有删除这个问题,我认为最好把它留给任何有同样问题的人,因为似乎没有答案互联网。

我能够通过以下代码解决问题:

public function bindNode() {
    $data = AuthComponent::user();
    return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
    }

它似乎完美无缺,也许它可以帮助其他人;)