关于托管的Symfony任务

时间:2011-07-08 14:52:57

标签: symfony1 symfony-1.4 task

我有任务:

<?php
require_once(dirname(__FILE__).'/../../config/ProjectConfiguration.class.php');

class GroupCheckTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

      $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));

    $this->namespace        = '';
    $this->name             = 'GroupCheck';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [GroupCheck|INFO] task does things.
Call it with:

  [php symfony GroupCheck|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {

    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    // add your code here



    $users=  Doctrine::getTable('sfGuardUserProfile')->getUserDelay() ;


       foreach($users as $sfGuardUserProfile)
       {
          $users_id =$sfGuardUserProfile-> getUserId();

          $userForChange =  Doctrine::getTable('sfGuardUserGroup')->getUserForChange($users_id) ;

          foreach($userForChange as $sfGuardUserGroup)
       {

            $sfGuardUserGroup->setSfGuardGroup(Doctrine::getTable('sfGuardGroup')-        >findOneByName('basic'))->save();

       }

       }
  }
}

在我的localhost中一切正常,但它不适用于托管。从cron选项卡运行托管任务的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

首先,您不需要放行require_once(dirname(__FILE__).'/../../config/ProjectConfiguration.class.php');

第二,为你的任务写一个命名空间是一个很好的做法

第三,你需要一个像以下的php文件:

<?php
  /* /path/to/file.php */
  chdir("/the/path/in/your/host/to/symfony/project");
  exec("symfony your_namespace:GroupCheck");
?>

最后,您可以将此文件放在crontab中

00 59 23 * * php /path/to/file.php