Php 如何使用特定标签向特定订阅者发送新闻?
我有一个订阅特定标签的订阅者数据库和需要发送给订阅者的新新闻。我需要在symfony4上写一个命令。我已经有了这个代码:Php 如何使用特定标签向特定订阅者发送新闻?,php,symfony,doctrine-orm,symfony4,Php,Symfony,Doctrine Orm,Symfony4,我有一个订阅特定标签的订阅者数据库和需要发送给订阅者的新新闻。我需要在symfony4上写一个命令。我已经有了这个代码: class SubscribeLauncherCommand extends ContainerAwareCommand { protected static $defaultName = 'app:subscribe-launcher'; private $mailer; protected $em; public function __c
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();
}
}
}
}
}
class SubscribeLauncherCommand扩展了ContainerWareCommand
{
受保护的静态$defaultName='app:subscribe-launcher';
私人$mailer;
受保护$em;
公共函数构造(EntityManagerInterface$em、\Swift\u Mailer$Mailer)
{
$this->mailer=$mailer;
$this->em=$em;
父项::_构造();
}
受保护的函数执行(InputInterface$input,OutputInterface$output)
{
$news=$this->em->getRepository(news::class)->findBy(array(),array('date_created'=>DESC');
$subscribers=$this->em->getRepository(newsubscribe::class)->findBy(数组('confirm'=>true));
$tags=$this->em->getRepository(Tag::class)->findAll();
$first_new_date=$news[0]->getDateCreated();
/**@var newsubscribe$订阅者*/
/**@var\Swift\u Message$Message*/
foreach($subscriber作为$subscriber){
foreach($news作为$new)
{
如果($new->getDateCreated()<$first\u new\u date){
$message=(新建\Swift\u消息(“测试电子邮件”)
->设置自('send@example.com')
->setTo($subscriber->getEmail())
->挫折体(
"测试",,
“文本/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...
}
}
}
}
foreach($subscribers作为$subscriber){
$subscribedTags=$subscriber->getSubscribedTags();
foreach($news作为$new)
{
如果($new->getDateCreated()<$first\u new\u date){
$relatedTag=$new->getTag();
if(在数组中($relatedTag,$subscribedTags)){//检查用户是否订阅了此新闻的特定标记
…发送电子邮件。。。
}
}
}
}
你能定义“不起作用”吗?您是否收到任何错误消息?你期待什么结果?问题是它不依赖标签和新新闻,我不知道如何将标签、新新闻和订阅者联系起来。它必须只发送新创建的带有特定标签的新闻。我希望它不能将其发送给所有电子邮件和所有订阅者,而只能是特定的。如果有带有两个标记的新闻,我们该怎么办?在这种情况下,您将使用:$relatedTags=$new->getTags();如果(sizeof(array_intersect($subscribedTags,$relatedTags)>0){…}谢谢,那太好了!