Hi everyone,
I’ve been using console commands for some time now from cron jobs mainly. They work just fine.
I’ve been trying for a specific project to catch incoming emails and process them upon certain conditions. Catching and filtering are done by cPanel, so I’m just using the ‘Pipe to a program’ action, like this:
|/path/to/protected/yiic processIncomingEmail
My issue: It seems that I can access only Yii::app() and nothing else “Yii” (ie I can’t access models or class constants).
Fortunately, I’ve implemented workarounds, but I’d like to know whether I’m doing something wrong or it’s by design, or even Yii-independent.
For instance:
<?php
class ProcessIncomingEmailCommand extends CConsoleCommand
{
public function run($args)
{
// The following works
$connection = Yii::app()->db;
$sql = // standard sql INSERT command;
$command = $connection->createCommand($sql);
$command->execute();
// The following doesn't work when in pipe mode, but works in cron mode
$model = new Model;
$model->attribute = 145;
$model->save();
// This works
$sql = 'UPDATE Model SET attribute = ' . Yii::app()->params['someConstant'] . ' WHERE id = 1234';
$command = $connection->createCommand($sql);
$command->execute();
// This doesn't work when in pipe mode, but works in cron mode
$sql = 'UPDATE Model SET attribute = ' . Model::SOME_CONSTANT . ' WHERE id = 1234';
$command = $connection->createCommand($sql);
$command->execute();
// Also Model::model()->find() etc. don't work in pipe mode
}
} ?>
Does it ring a bell to anyone?
Thanks in advance!
A.