我正在用symfony2构建一个web应用程序,我有一个问题。用户订阅了一个事件,我希望我的应用程序在事件发生前一天自动通知他。我有必要的表格;用户,事件,inscri_event,它代表用户和事件之间的关联,持有额外的字段,我已经实现了一个服务来通知作为参数传递的用户列表。
如何告诉symfony自动检查当前日期,一旦它在事件日期的前一天,通知订阅的用户。
请告诉我我到底需要做什么,或者至少给我指出正确的道路。我都不知道该在谷歌上输入什么。我是Symfony 2的新手,所以请原谅我。提前谢谢你。这是我的函数
public function notifierAction()
{
$em = $this->getDoctrine()->getManager();
$seances = $em->getRepository('OCUserBundle:Seance')->findAll();
foreach ($seances as $s)
{
$datenow= new DateTime();
$interval = $datenow->diff($s->getDate(),true)->format('d');
if ($interval=1 )
{
$inscritformation = $em->getRepository('OCUserBundle:InscriFormation')->findBy($s->getFormation()->getId());
$notificateur = $this->get('Notificateur');
$repository2 = $this->getDoctrine()->getManager()->getRepository('OCUserBundle:UserApprenant');
foreach ($inscritformation as $insfor) {
$user = $insfor->getApprenant()->getId();
$apprenant = $repository2->findBy(array('id' => $user));
$notificateur->notifier('la formation dont vous ete inscrit est demain' . $s->getFormation()->getTitre(), $apprenant, $s->getFormation()->getId(),'alarme');
}
}}}
您需要创建一个Command
并与cron
一起运行它。例如:
class NotifyUsersCommand extends Command
{
public function configure()
{
$this
->setName('notify:users')
// more configuration
;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getDoctrine()->getManager();
// Find all users with event date = tomorrow
$seances = $em->getRepository('OCUserBundle:Seance')
->findAllEventsWithDateTomorrow();
foreach ($seances as $s) {
// send notifications
}
}
}
在SeanceRepository(或任何存储库名称)
public function findAllEventsWithDateTomorrow()
{
$tomorrow = new DateTime();
$tomorrow->modify('+1 day'); // 1 day from now
$qb = $this->getEntityManager()
->createQueryBuilder()
->where('u.date < :tomorrow')
->setParameter(':tomorrow', $tomorrow->format(/*datetime format*/));
return $qb->getQuery()->getResult();
}
然后配置cron每分钟运行一次命令(检查您的php二进制路径与which php
)
# crontab -e
* * * * * /usr/bin/php /path/to/your/app/console notify:users
Crontab参考http://www.adminschoice.com/crontab-quick-reference
如果您使用的是Windows,则cron等效为schtasks
schtasks /create /sc minute /mo 1 /tn "Notify users" /tr "C:pathtophp.exe C:pathtoyourappconsole notify:users"
schtasks reference https://technet.microsoft.com/en-us/library/cc725744.aspx