【问题标题】:Yii use model in a commandYii 在命令中使用模型
【发布时间】:2014-03-26 03:41:21
【问题描述】:

我有一个 Yii 应用程序,我正在尝试向它添加一个命令。

protected/commands/SendMessageCommand.php

class SendMessageCommand extends CConsoleCommand {
    public function run($args) {
        $messages = Message::model()->findAll(array('type' => 'S', 'sent' => null, 'scheduled_for' => array('$ne' => null)));
        [....]

/protected/config/console.php

[....]
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Phrizz Console',

'preload'=>array('log'),

    'import'=>array(
        'application.models.*',
        'application.forms.*',
        [....]

当我尝试运行时

$ yiic sendmessage

我来了

PHP Fatal error:  Class 'Message' not found in /protected/commands/SendMessageCommand.php on line 7

如何在命令行脚本中访问模型?

Yii 1.1.4

【问题讨论】:

  • 澄清一下,Message 是在整个应用程序中使用的有效模型
  • 尝试在您的命令run 方法中使用Yii::import('application.models.Message'),以确保没有发生配置冲突
  • 我做到了,不高兴。几分钟前我已经解决了。请看我的回答。

标签: php yii model console


【解决方案1】:

Windows 7 出现问题。解决方法:

class SendMessageCommand extends CConsoleCommand {
    public function run($args) {
        Yii::$enableIncludePath = false;
        $messages = Message::model()->findAll(array('type' => 'S', 'sent' => null, 'scheduled_for' => array('$ne' => null)));
        [....]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多