dongqigu0429 2014-01-06 16:46
浏览 37
已采纳

在Symfony 1.4中的Action中调用的“嵌套”任务给出了错误

I have a bunch of symfony tasks to send different kind of emails. For example, I have a sendMailConfirmationTask, sendMailAlertContactTask, sendMailBlogTask etc...

My goal is to have one "master" class : sendMailBaseTask, that will, based on the correct parameter, execute the right task.

So as an example, when I execute "php symfony sendMail:base --object=confirmation", it creates an instance of sendMailBaseTask, and then, the following line makes the call to the right task :

 $this->runTask($prefix . $task_name, array(), $options); // $prefix . $task_name  = "sendMail:confirmation" for example

Through the CLI, both methods works fine, I can trigger my confirmation email these ways :

php symfony sendMail:base --object=confirmation --to=some@email.com

php symfony sendMailConfirmation --to=some@email.com

Where it becomes tricky is when I want to run my task within an sfAction. I'd like to run my task whenever someone registers onto my application for example. So here's the piece of code I've tried without luck :

chdir(sfConfig::get('sf_root_dir'));
$task = new sendMailBaseTask($configuration->getEventDispatcher(), new sfFormatter());
$rc = $task->run(array(), array('object' => 'confirmation', 'to' => $email, 'hash' => $hash));
chdir($current_dir);

This gives the following error : "Unable to create a task as no command application is associated with this task yet."

But, if instead of creating a sendMailBaseTask instance I create a sendMailConfirmationTask, it works fine.

I could do it that way, but that's not the way I want it working, so if anyone has a clue... Thanks!

  • 写回答

1条回答 默认 最新

  • donglei1699 2014-01-19 21:29
    关注

    It seems that you need to launch your task in a different way.

    If we take a look at how doctrine handle this case with the doctrine:build task. It does that:

    if (self::BUILD_MODEL == (self::BUILD_MODEL & $mode))
    {
      $task = new sfDoctrineBuildModelTask($this->dispatcher, $this->formatter);
      $task->setCommandApplication($this->commandApplication);
      $task->setConfiguration($this->configuration);
      $ret = $task->run();
    
      if ($ret)
      {
        return $ret;
      }
    }
    

    This is launched when you type:

    php symfony doctrine:build --model
    

    This means, your sendMailBaseTask shouldn't use runTask but something like that:

    $task = new sendMailConfirmationTask($this->dispatcher, $this->formatter);
    $task->setCommandApplication($this->commandApplication);
    $task->setConfiguration($this->configuration);
    $ret = $task->run(array(), $options);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入