我有订阅者的数据库,订阅了特定的标签和需要发送给订阅者的新新闻。我需要在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...
}
}
}
}