Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a console command in web application action in Yii?

I have a console command to do a consumer time, AND I need to know how to call (execute) it in a web application action in YII.

class MyCommand extends CConsoleCommand{
      public function actionIndex(){
          $model = new Product();
          $model->title = 'my product';
          ...
          $model->save();
          .
          .
          .
      }
}

I want to execute this code.

like image 486
JahangirAhmad Avatar asked Apr 14 '12 07:04

JahangirAhmad


1 Answers

try this:

    Yii::import('application.commands.*');
    $command = new MyCommand("test", "test");
    $command->run(null);

The 2 parameters with value "test" must be set but do not have an impact, they are used for the --help option when using the console.

/**
 * Constructor.
 * @param string $name name of the command
 * @param CConsoleCommandRunner $runner the command runner
 */
public function __construct($name,$runner)
{
    $this->_name=$name;
    $this->_runner=$runner;
    $this->attachBehaviors($this->behaviors());
}

https://github.com/yiisoft/yii/blob/master/framework/console/CConsoleCommand.php#L65

like image 120
Sebastian Viereck Avatar answered Sep 19 '22 01:09

Sebastian Viereck