2014-03-26 21 views
1

我有一個Yii應用程序,我正在嘗試向它添加一個命令。Yii在命令中使用模型

保護/命令/ 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

+0

只是爲了澄清,消息是所有使用過的應用程序 –

+0

嘗試'Yii的一個有效模式::進口( 'application.models.Message')'在你的命令'run'方法中,只是爲了確保沒有配置衝突 –

+0

我做了,沒有快樂。我幾分鐘前解決了它。請看我的答案。 –

回答

2
在Windows 7

療法問題appers解決方案:

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))); 
     [....]