3

正如标题已经说的那样,我在 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:对于喜欢我的问题的德语版本的每个人,这里

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

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

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

4

1 回答 1

4

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

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

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

它似乎工作得很好,也许它可以帮助其他一些人;)

于 2012-11-19T19:15:15.833 回答