Question

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!

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top