Scheduling Tasks to run in a Yii environment


                          I have been lucky enough to have been  given an opportunity to work with Yii and PHP using a SQL Server  backend. I have developed a number of applications with simple workflow  and so far so good. I now require to have programs run that read through  the records created by the application to send out reminders, update  status information etc.

                          I was wondering if someone could please  give me some direction on how I might go about setting this up. The Yii,  PHP environment runs on a Windows 2008 server so I am guessing I can  use Windows Scheduler - I have never used this before. Is it just a  matter of writing an action in the Controller I wish to run and then  pointing a Windows Scheduled task to that URL eg [url="http://......./index.php/myapp/myAppsAction?"]http://......./index.php/myapp/myAppsAction?[/url]

                          My search on the Web directed me to running Cron jobs and even that seemed confusing.


                          Kind regards


hey, not a big deal.

A bit tricky but easy solution provided from Yii is to have a console command, for understanding may you call command = controller and action in it.

for details may follow


let me now if you have trouble in implementation.

once a command is ready you may set a schedule job or just call it from script.


You have create a separete commands to set sechedule.

1)Add setup to console.php for your database connection, which is inside Config folder


return array(


'name'=&gt;'My Console Application',








		'connectionString' =&gt; 'mysql:host=localhost;dbname=yourdatabasename',

		'emulatePrepare' =&gt; true,

		'username' =&gt; '',

		'password' =&gt; '',

		'charset' =&gt; 'utf8',

		'tablePrefix' =&gt; 'tbl_',






2)Create folder ‘commands’ inside protected

3)inside folder create your command file as bellow MaintainCommand.php



class MaintainCommand extends CConsoleCommand


    public function actionrunnow(&#036;id){

// You can do your action here




  1. Create the schedule job for this command function it will work fine.

or you can use below code to execute

$commandPath = Yii::app()->getBasePath() . DIRECTORY_SEPARATOR . ‘commands’;

&#036;runner = new CConsoleCommandRunner();


&#036;commandPath = Yii::getFrameworkPath() . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'commands';


&#036;args = array('yiic', 'Maintain', 'runnow','--id=10');//--id is the argument we are passing we can send multiple argument 



echo htmlentities(ob_get_clean(), null, Yii::app()-&gt;charset);