我有一个数据库,其中包含订阅特定标签的订阅者和需要发送给订阅者的新新闻。我需要在 Symfony 4 上编写命令来完成它。我已经有了这个代码:
class SubscribeLauncherCommand extends ContainerAwareCommand
{
protected static $defaultName = 'app:subscribe-launcher';
private $mailer;
protected $em;
public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer)
{
$this->mailer = $mailer;
$this->em = $em;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$news = $this->em->getRepository(News::class)->findBy(array(), array('date_created' => 'DESC'));
$subscribers = $this->em->getRepository(NewsSubscribe::class)->findBy(array('confirmed' => true));
$tags = $this->em->getRepository(Tag::class)->findAll();
$first_new_date = $news[0]->getDateCreated();
/** @var NewsSubscribe $subscribers */
/** @var \Swift_Message $message */
foreach ($subscribers as $subscriber) {
foreach ($news as $new)
{
if ($new->getDateCreated() < $first_new_date) {
$message = (new \Swift_Message('Test Email'))
->setFrom('send@example.com')
->setTo($subscriber->getEmail())
->setBody(
'test',
'text/html');
$first_new_date = $new->getDateCreated();
}
}
}
}
}
但它不起作用。你能帮忙吗?
最佳答案
所以我认为订阅者有一组他们订阅的标签,新闻与标签相关。
如果是这种情况,您需要在循环中添加几个条件,例如:
foreach ($subscribers as $subscriber) {
$subscribedTags = $subscriber->getSubscribedTags();
foreach ($news as $new)
{
if ($new->getDateCreated() < $first_new_date) {
$relatedTag = $new->getTag();
if(in_array($relatedTag, $subscribedTags)){ //Check if the user is subscribed to the particular tag of this news
...Send the email...
}
}
}
}
关于php - 如何将新闻发送给带有特定标签的特定订阅者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628127/