如何将新闻发送给具有特定标签的特定订户?

时间:2019-06-17 09:07:23

标签: php symfony doctrine-orm symfony4

我有一个包含订阅者的数据库,这些订阅者订阅了特定的标记和需要发送给订阅者的新新闻。我需要在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();
                }
            }
        }
    }
}

但是它不起作用。你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

因此,我认为订户具有他们所订阅的标签的集合,而新闻与该标签有关。

如果是这种情况,则需要在循环中添加几个条件,例如:

 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...
                }
            }
        }
    }