[英]Yii - How to access to model and call action in yiic
I have an action that prints "Hello World": 我有一个打印“ Hello World”的动作:
public function actionStart()
{
echo 'Hello World';
}
I will to run a cron job that calls this action every minute. 我将运行一个cron作业,每分钟调用一次此操作。
How I can do this with yiic ? 我如何用yiic做到这一点?
I create console app. 我创建控制台应用程序。 I have cron.php inside index.php: 我在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();
and config/c.php: 和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',
// …
),
),
);
and protected/commands/MyCommand.php: 和protected / commands / MyCommand.php:
<?php
class MyCommand extends CConsoleCommand
{
public function run($args)
{
$logs = Log::model()->findAll();
print_r($logs);
die();
}
}
when I run this: 当我运行这个:
$ ./protected/yiic my
I got this error: 我收到此错误:
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()
How I can fix this? 我该如何解决?
First you have to create a command: http://www.yiiframework.com/doc/guide/1.1/en/topics.console#creating-commands 首先,您必须创建一个命令: http : //www.yiiframework.com/doc/guide/1.1/en/topics.console#creating-commands
Then you can acess your function by running this command: yiic yourCommand start 然后,您可以通过运行以下命令来访问函数:yiic yourCommand start
You have to load your models in Console application. 您必须在控制台应用程序中加载模型。
Open console.php
configuration file, and add this line under the line 'name'=>'My Console Application',
打开console.php
配置文件,并在'name'=>'My Console Application',
行下添加此行'name'=>'My Console Application',
'import'=>array(
'application.models.*'
),
It should work. 它应该工作。
It seems that your don't have such 'log.php' file you are including. 似乎您没有要包含的“ log.php”文件。
maybe try this from console : 也许从控制台尝试:
touch /path/to/app/folder/protected/log.php
You should have another error message then (or it will run smoothly). 然后,您应该会收到另一条错误消息(否则它将平稳运行)。
只需在其中打开yiic.php文件检查框架路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.