Yii - How to access to model and call action in yi

2019-02-26 07:36发布

I have an action that prints "Hello World":

public function actionStart()
{
    echo 'Hello World';
}

I will to run a cron job that calls this action every minute.

How I can do this with yiic ?


UPDATE:

I create console app. I have cron.php inside index.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:

<?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:

<?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?

标签: yii cron yiic
4条回答
时光不老,我们不散
2楼-- · 2019-02-26 07:56

First you have to create a command: 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

查看更多
孤傲高冷的网名
3楼-- · 2019-02-26 08:01

Just open your yiic.php file check framework path in it.

查看更多
三岁会撩人
4楼-- · 2019-02-26 08:03

It seems that your don't have such 'log.php' file you are including.

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).

查看更多
孤傲高冷的网名
5楼-- · 2019-02-26 08:08

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',

'import'=>array(
        'application.models.*'
    ),

It should work.

查看更多
登录 后发表回答