Yii2: i want to add a child permission for a parent permission, but i saw a bug( i think )

Created on 19 Sep 2017  ·  3Comments  ·  Source: yiisoft/yii2

What steps will reproduce the problem?

    $auth = Yii::$app->authManager;

    $parent = $auth->getPermission("/test/one");

    $child = $auth->createPermission("/test/two");
    $child->description = "这是one的子类";

    $child_is = $auth->addChild($parent, $child);

What is the expected result?

    我想给父权限项(也就是菜单) 添加子权限项(子菜单)

What do you get instead?

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (testyii.auth_item_child, CONSTRAINT auth_item_child_ibfk_2 FOREIGN KEY (child) REFERENCES auth_item (name) ON DELETE CASCADE ON UPDATE CASCADE)
The SQL being executed was: INSERT INTO auth_item_child (parent, child) VALUES ('/test/one', '/test/two')

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 7.0.23
| Operating system | win7

please, help me, thank you , everyone

question

Most helpful comment

You forgot to add the $child permission into the RBAC table before declaring relations using addChild():

$child = $auth->createPermission("/test/two");
$child->description = "这是one的子类";

// The folllowing line is missing
$auth->add($child);

$child_is = $auth->addChild($parent, $child);

All 3 comments

You forgot to add the $child permission into the RBAC table before declaring relations using addChild():

$child = $auth->createPermission("/test/two");
$child->description = "这是one的子类";

// The folllowing line is missing
$auth->add($child);

$child_is = $auth->addChild($parent, $child);

Thank you for your question.
In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

_This is an automated comment, triggered by adding the label question._

thank you @drodata

Was this page helpful?
0 / 5 - 0 ratings