$auth = Yii::$app->authManager;
$parent = $auth->getPermission("/test/one");
$child = $auth->createPermission("/test/two");
$child->description = "这是one的子类";
$child_is = $auth->addChild($parent, $child);
我想给父权限项(也就是菜单) 添加子权限项(子菜单)
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')
| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 7.0.23
| Operating system | win7
please, help me, thank you , everyone
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
Most helpful comment
You forgot to add the
$childpermission into the RBAC table before declaring relations usingaddChild():