Yii Command Line Does Not Autoload Models

杀马特。学长 韩版系。学妹 提交于 2019-12-07 09:28:10

问题


I'm trying to run a command on the command line with Yii. My method requires a model but when I run the command, Yii says model does not exist, as if it is not auto-loading it. My class is fairly short and simple:

class ImportCommand extends CConsoleCommand {

    public function actionIndex() {

    }

    public function actionImport() {

        ini_set("display_errors", 1);
        set_time_limit(0);

        $cores = Core::model() -> findAll(array('limit' => 300000));

        foreach($cores as $core) {
            $core -> syncLocation();
        }


    }
}

The error message looks like this:

./yiic import import
PHP Warning:  PHP Startup: Unable to load dynamic library '/opt/local/lib/php/extensions/no-debug-non-zts-20090626/php_xsl.dll' - dlopen(/opt/local/lib/php/extensions/no-debug-non-zts-20090626/php_xsl.dll, 9): image not found in Unknown on line 0
PHP Error[2]: include(Core.php): failed to open stream: No such file or directory
    in file /Users/dixond/Sites/nevada/yii/framework/YiiBase.php at line 421
#0 /Users/dixond/Sites/nevada/yii/framework/YiiBase.php(421): autoload()
#1 unknown(0): autoload()
#2 /Users/dixond/Sites/nevada/pub/webservice/protected/commands/ImportCommand.php(15): spl_autoload_call()
#3 unknown(0): ImportCommand->actionImport()
#4 /Users/dixond/Sites/nevada/yii/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs()
#5 /Users/dixond/Sites/nevada/yii/framework/console/CConsoleCommandRunner.php(67): ImportCommand->run()
#6 /Users/dixond/Sites/nevada/yii/framework/console/CConsoleApplication.php(91): CConsoleCommandRunner->run()
#7 /Users/dixond/Sites/nevada/yii/framework/base/CApplication.php(169): CConsoleApplication->processRequest()
#8 /Users/dixond/Sites/nevada/yii/framework/yiic.php(33): CConsoleApplication->run()
#9 /Users/dixond/Sites/nevada/pub/webservice/protected/yiic.php(7): require_once()
#10 /Users/dixond/Sites/nevada/pub/webservice/protected/yiic(4): require_once()

Is there something special I have to do to get Yii to autoload models?


回答1:


The yiic command uses usually a different config file than your web application.

Define the needed paths in the import section of your config/console.php, like you have it in your config/main.php.




回答2:


Write this code in your config/console.php
// autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),

for more detail http://www.yiiframework.com/doc/guide/1.1/en/topics.console



来源:https://stackoverflow.com/questions/15615888/yii-command-line-does-not-autoload-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!