我试图通过一次从数据库10中提取电子邮件来向我的订阅者列表发送电子邮件。我希望能够通过控制器操作暂停和恢复发送这些电子邮件。
Symfony中是否有任何方法(或者这可能是一般的PHP问题)来控制另一个操作的控制器操作?像这样:
public function sendEmailAction()
{
// loop through recipients and send emails
}
public function pauseEmailAction()
{
// pause the loop in sendEmail
}
public function resumeEmailAction()
{
// resume sendEmailAction from the point where
// pauseEmailAction has stopped it
}
答案 0 :(得分:0)
我可能会遗漏一些东西,但这应该足以满足您的需求。
public function firstAction()
{
for (i=0; i < 100; i++) {
$users = $this->giveMeTenUsers($i);
$this->secondAction($users);
}
}
public function secondAction(array $users)
{
// do stuff like send the emails
return;
}
当firstAction
调用secondAction
时,firstAction
的执行被停止&#34;等待secondAction
的结果。当secondAction
命中return;
时,它会结束自己的执行,从而将程序发送回firstAction
,在调用它的循环内。
这就是你需要的吗?还是有一个我没有得到的元素?
编辑:为什么不尝试设置布尔值来中断?
像这样:
public function mainAction() // firstAction
{
for (i=0; i < 100; i++) {
while ($this->isInterrupted()) {}
$users = $this->giveMeTenUsers($i);
// Do stuff
}
}
public function interruptAction() // secondAction
{
$this->setInterrupted(true);
}
public function releaseAction() // thirdAction
{
$this->setInterrupted(false);
}
编辑2:顺便说一句,您不必使用操作,它适用于任何类型的方法。
编辑3:在课堂上添加
private $interrupted = false;
public function isInterrupted()
{
return $this->interrupted;
}
public setInterrupted(bool $interrupted)
{
$this->interrupted = $interrupted;
}