【发布时间】:2014-10-15 05:11:30
【问题描述】:
我想安排在 yii 中运行控制台命令。 我编写了 php 脚本并在 web-app 中尝试了它 - 效果很好。 然而,当我将其设为console command 时,cron 会发送以下消息:
PHP Error[2]: include(User.php): failed to open stream: No such file or directory
in file /home/srv50213/htdocs/framework/YiiBase.php at line 427
#0 /home/srv50213/htdocs/framework/YiiBase.php(427): autoload()
#1 unknown(0): autoload()
#2 /home/srv50213/htdocs/doc/protected/commands/ReportMailerCommand.php(20): spl_autoload_call()
#3 unknown(0): ReportMailerCommand->actionIndex2()
#4 /home/srv50213/htdocs/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs()
#5 /home/srv50213/htdocs/framework/console/CConsoleCommandRunner.php(71): ReportMailerCommand->run()
#6 /home/srv50213/htdocs/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()
#7 /home/srv50213/htdocs/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#8 /home/srv50213/htdocs/framework/yiic.php(33): CConsoleApplication->run()
#9 /home/srv50213/htdocs/doc/protected/yiic.php(8): require_once()
似乎 autoload() 无法正常工作。 对于控制台应用程序 $config 我使用 config/console.php 我在其中插入了 db 设置(来自 main.php 和用户设置)。
'components'=>array(
'db'=>array(
// настройки для конфигурации
'connectionString' =>
'mysql:host=localhost;dbname=xxx',
'emulatePrepare' => true,
'schemaCachingDuration' => 3600,
'username' => 'xxx',
'password' => 'xxx',
'charset' => 'utf8',
'tablePrefix' => 'yiiapp_',
'class' => 'CDbConnection'
),
'user'=>array(
'class' => 'WebUser',
//'class' => 'application.components.User',
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
)
脚本是smth。像这样:
public function actionIndex2($args)
{
$users = User::model()->findAllByPk( array(177) );
foreach($users as $user)
{
$query = "SELECT e.id, e.Subject
FROM `yiiapp_doc_events` AS e
LEFT JOIN `yiiapp_doc_event_users` AS u ON e.id = u.eventId
WHERE e.StatusId NOT IN (5,7)
AND u.userId = {$user->id}
GROUP BY e.id";
$queryResult = Yii::app()->db->createCommand($query)->queryAll();
if ($queryResult)
{
foreach($queryResult as $res)
{
$i++;
$link = Yii::app()->createAbsoluteUrl('docEvents/update', array('id'=>$res['id']));
$table .= "<tr><td> {$i} </td><td> <a href='{$link}'>{$res['id']}</a> </td><td> <a href='{$link}'>{$res['Subject']}</a> </td></tr>";
}
//echo $table . '</table>';
$content= " $table . '</table>';
mail($user->email, "Report (". $i . ')', $content, 'Content-type: text/html');
}
}
}
怎么了,怎么解决?
更新
正如 chris 所建议的,我在配置数组中添加了导入参数。
现在这个问题没有了。然而,由于我呼吁Yii::app()->createAbsoluteUrl(),另一件事让Undefined index: SERVER_NAME感到沮丧:
PHP Error[8]: Undefined index: SERVER_NAME
in file /home/srv50213/htdocs/framework/web/CHttpRequest.php at line 317
#0 /home/srv50213/htdocs/framework/base/CApplication.php(560): CHttpRequest->getHostInfo()
#1 /home/srv50213/htdocs/doc/protected/commands/ReportMailerCommand.php(39): CConsoleApplication->createAbsoluteUrl()
...
解决办法是什么?
解决方案
在控制台命令中搜索 createAbsoluteUrl 后,我发现了有用的线程 1 和 2。
第一个给出了这个补救措施 - 在 config/console.php 中添加以下内容:
'components' => array(
'request' => array(
'hostInfo' => 'https://domainname.com',
'baseUrl' => '',
'scriptUrl' => '',
),
...
)
第二种方式类似:
'components' => array(
'urlManager' => array(
'baseUrl' => 'https://domain.com'
)
)
【问题讨论】:
标签: php yii command console-application