如何调试此错误:Property";CDbAuthManager.connectionId”;未定义


How can I debug this error: Property "CDbAuthManager.connectionId" is not defined

我正在使用yi 1.1应用程序开发食谱,并尝试实现RBAC(基于角色的访问控制)。我得到的错误表明CDbAuthManager没有定义,这很奇怪,因为我测试了数据库连接,这很有效,我正在main.php配置文件中定义数据库。这是代码main.php:

// application components
'components'=>array(
    'user'=>array(
        // enable cookie-based authentication
        'allowAutoLogin'=>true,
    ),
        'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=rbac',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => 'myPassword',
        'charset' => 'utf8',
    ),
    'authManager'=>array(
        'class'=>'CDbAuthManager',
        'connectionId'=>'db',
        ),

这是RbacController.php文件:

<?php 
class RbacController extends CController
{
        public function filters()
        {
            return array(
                'accessControl',
                );
        }
        public function accessRules()
        {
            return array(
                array(
                    'allow',
                    'actions'=>array('deletePost'),
                    'roles'=>array('deletePost'),
                    ),
                    array(
                        'allow',
                        'actions'=>array('init', 'test'),
                        ),
                        array('deny'),
                );
        }
        public function actionInit()
        {
            $auth=Yii::app()->authManager;
            $auth->createOperation('createPost', 'create a post');
            $auth->createOperation('readPost', 'read a post');
            $auth->createOperation('updatePost', 'update a post');
            $auth->createOperation('deletePost', 'delete a post');
            $bizRule='return Yii::app()->user->id==$params["post"]->authId;';
            $task=$auth->createTask('updateOwnPost', 'update a post by author himself', $bizRule);
            $task->addChild('updatePost');
            $role=$auth->createRole('reader');
            $role->addChild('readPost');
            $role=$auth->createRole('author');
            $role->addChild('readPost');
            $role->addChild('createPost');
            $role->addChild('updateOwnPost');
            $role=$auth->createRole('editor');
            $role->addChild('readPost');
            $role->addChild('createPost');
            $role=$auth->createRole('admin');
            $role->addChild('editor');
            $role->addChild('author');
            $role->addChild('deletePost');
            $auth->assign('reader', 'readerA');
            $auth->assign('author', 'authorB');
            $auth->assign('editor', 'editorC');
            $auth->assign('admin', 'adminD');
            echo 'Done';
        }
        public function actionDeletePost()
        {
            echo "Post Deleted";
        }
        public function actionTest()
        {
            $post= new stdClass();
            $post->authId = 'authorB';
            echo "Current Permissions:<br />";
            echo "<ul>";
            echo "<li>Create post: ".Yii::app()->user->checkAccess('createPost')."</li>";
            echo "<li>Read post: ".Yii::app()->user->checkAccess('readPost')."</li>";
            echo "<li>Update post: ".Yii::app()->user->checkAccess('updatePost')."</li>";
            echo "<li>Delete post: ".Yii::app()->user->checkAccess('deletePost')."</li>";
            echo "</ul>";
        }
}
 ?>

我的印象是,当使用数据库版本的authManager"class"=>"DbAuthManager"时,设置层次结构的唯一方法是通过yiic。但它在书中没有提到yiic,只是说"现在运行init一次以创建RBAC层次结构"。对我来说,这意味着在url中键入控制器/操作对,如下所示:http://localhost/rbac/index.php?r=Rbac/init.IDK,也许这就是我做错的地方。他们在网上搜索了一下,并没有发现任何类似的问题。

编辑:我只是尝试了另一种方法。这一次,我在/Users/scottmcpherson/Sites/phpsites/rbac/protected/commands/shell/RbacCommand.php中创建了一个控制台应用程序命令当我试图在终端中运行它时,我会遇到同样的错误:

exception 'CException' with message 'Property "CDbAuthManager.connectionId" is   not defined.' in    /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CComponent.php:174
Stack trace:
#0 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/YiiBase.php(225):   CComponent->__set('connectionId', 'db')
#1  /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CModule.php(371):  YiiBase::createComponent(Array)
#2  /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CModule.php(86):  CModule->getComponent('authManager')
#3 /Users/scottmcpherson/Sites/phpsites/rbac/protected/commands/shell/RbacCommand.php (24): CModule->__get('authManager')
#4  /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/cli/commands/ShellCommand.p hp(147): RbacCommand->run(Array)
#5  /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/cli/commands/ShellCommand.p hp(99): ShellCommand->runShell()
#6 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/console/CConsoleCommandRunner.php(63): ShellCommand->run(Array)
#7 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/console/CConsoleApplication .php(88): CConsoleCommandRunner->run(Array)
#8  /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/base/CApplication.php(158):     CConsoleApplication->processRequest()
#9 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/yiic.php(33):  CApplication->run()
#10 /Users/scottmcpherson/Sites/phpsites/yiiRoot/framework/yiic(15):  require_once('/Users/scottmcp...')
#11 {main}

它说connectionId没有定义,但我已经定义了它,并将其设置为有效的数据库连接。它怎么能不被定义呢?

我承认,这个错误有点模糊。事实恰恰相反。您正试图设置connectionId,但错误告诉您不能这样做,因为connectionId不是CDbAuthManager中的有效属性。我相信它实际上是:

'connectionID' => 'db',

"d"也必须大写。

我希望这会有所帮助,这确实不是最明确的错误信息。