php - 如何将新闻发送给带有特定标签的特定订阅者?

标签 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();
                }
            }
        }
    }
}

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

最佳答案

所以我认为订阅者有一组他们订阅的标签,新闻与标签相关。

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

 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/

相关文章:

javascript - 带有 Ratchet 的 PHP WebSockets - 示例不起作用

javascript - 当 php 脚本通过 ajax 运行时显示进度条

symfony - 如何将存储库注入(inject)服务

sql - 是否可以在 Symfony3 中的实体内定义自定义 SQL 查询

doctrine-orm - Doctrine ORM "Multiple non-persisted new entities were found through the given association graph:"

使用 constant() 时出现 PHP fatal error

php - vbulletin 4 搜索不起作用

php - 两个实体之间的 Symfony2 关系

Mysql Doctrine2外键失败

php - Symfony2 : Base table or view not found: 1146