Как отправить новости конкретным подписчикам с определенными тегами? - PullRequest
2 голосов
/ 17 июня 2019

У меня есть база данных с подписчиками, которые подписаны на определенные теги и новые новости, которые необходимо отправлять подписчикам.Мне нужно написать команду на 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 голосов
/ 17 июня 2019

Итак, я полагаю, подписчики имеют коллекцию тегов, на которые они подписаны, а новости связаны с тегом.

Если это так, вам нужно добавить пару условий в ваш цикл, что-то вроде:

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