[英]Yii - How to access to model and call action in yiic
我有一個打印“ Hello World”的動作:
public function actionStart()
{
echo 'Hello World';
}
我將運行一個cron作業,每分鍾調用一次此操作。
我如何用yiic做到這一點?
我創建控制台應用程序。 我在index.php中有cron.php:
<?php
defined('YII_DEBUG') or define('YII_DEBUG',true);
// including Yii
require_once('framework/yii.php');
// we'll use a separate config file
$configFile='protected/config/c.php';
// creating and running console application
Yii::createConsoleApplication($configFile)->run();
和config / c.php:
<?php
return array(
// This path may be different. You can probably get it from `config/main.php`.
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'Cron',
'preload'=>array('log'),
'import'=>array(
'application.models.*',
'application.components.*',
),
// We'll log cron messages to the separate files
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'logFile'=>'cron.log',
'levels'=>'error, warning',
),
array(
'class'=>'CFileLogRoute',
'logFile'=>'cron_trace.log',
'levels'=>'trace',
),
),
),
// Your DB connection
'db'=>array(
'class'=>'CDbConnection',
// …
),
),
);
和protected / commands / MyCommand.php:
<?php
class MyCommand extends CConsoleCommand
{
public function run($args)
{
$logs = Log::model()->findAll();
print_r($logs);
die();
}
}
當我運行這個:
$ ./protected/yiic my
我收到此錯誤:
PHP Error[2]: include(Log.php): failed to open stream: No such file or directory
in file /path/to/app/folder/framework/YiiBase.php at line 427
#0 /path/to/app/folder/framework/YiiBase.php(427): autoload()
#1 unknown(0): autoload()
#2 /path/to/app/folder/protected/commands/MyCommand.php(6): spl_autoload_call()
#3 /path/to/app/folder/framework/console/CConsoleCommandRunner.php(71): MyCommand->run()
#4 /path/to/app/folder/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()
#5 /path/to/app/folder/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#6 /path/to/app/folder/framework/yiic.php(33): CConsoleApplication->run()
#7 /path/to/app/folder/protected/yiic.php(7): require_once()
#8 /path/to/app/folder/protected/yiic(3): require_once()
我該如何解決?
首先,您必須創建一個命令: http : //www.yiiframework.com/doc/guide/1.1/en/topics.console#creating-commands
然后,您可以通過運行以下命令來訪問函數:yiic yourCommand start
您必須在控制台應用程序中加載模型。
打開console.php
配置文件,並在'name'=>'My Console Application',
行下添加此行'name'=>'My Console Application',
'import'=>array(
'application.models.*'
),
它應該工作。
似乎您沒有要包含的“ log.php”文件。
也許從控制台嘗試:
touch /path/to/app/folder/protected/log.php
然后,您應該會收到另一條錯誤消息(否則它將平穩運行)。
只需在其中打開yiic.php文件檢查框架路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.