首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swiftmailer: dbReplacements by user id?

Swiftmailer: dbReplacements by user id?
EN

Stack Overflow用户
提问于 2012-11-04 16:42:55
回答 2查看 250关注 0票数 0

Swiftmailer文档说,您可以使用Decorator插件创建自己的类来处理替换:

代码语言:javascript
运行
复制
class DbReplacements implements Swift_Plugins_Decorator_Replacements {
  public function getReplacementsFor($address) {
    $sql = sprintf(
      "SELECT * FROM user WHERE email = '%s'",
      mysql_real_escape_string($address)
    );

    $result = mysql_query($sql);

    if ($row = mysql_fetch_assoc($result)) {
      return array(
        '{username}'=>$row['username'],
        '{password}'=>$row['password']
      );
    }
  }
}

但在我的情况下,我的数据库包含重复的电子邮件地址,即相同的地址可能出现在3-4个帐户上,因此我需要根据用户id获取替换地址。

如何修改上面的类以匹配我的条件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-04 17:33:10

由于swiftmailer对id一无所知,您必须自己将电子邮件转换为id。例如,将新属性添加到包含关联数组'email' => 'id'DbReplacements (当然,首先限制为已知ID,即。SELECT email, id FROM user WHERE id IN(1,3,6,77) ),而在getReplacementsFor中,只需使用电子邮件作为此数组中的索引,即可获得用户id。

代码示例以使其更清晰:

代码语言:javascript
运行
复制
class DbReplacements implements Swift_Plugins_Decorator_Replacements {
  public $email2id = array();
  public function getReplacementsFor($address) {
    $sql = sprintf(
      "SELECT * FROM user WHERE id = %d", $this->email2id[$address]
    );

    $result = mysql_query($sql);

    if ($row = mysql_fetch_assoc($result)) {
      return array(
        '{username}'=>$row['username'],
        '{password}'=>$row['password']
      );
    }
  }
}

$dbReplacer = new DbReplacements();
$decorator = new Swift_Plugins_DecoratorPlugin($dbReplacer); 
$mailer->registerPlugin($decorator);

$users = array(
    array('email'=>'john.doe@example.com', 'id' => 16),
    array('email'=>'john.doe2@example.com', 'id' => 13),
);

foreach ($users as $user) {
  $message->addTo($user['email']);
  $dbReplacer->email2id[$user['email']] = $user['id'];
}
票数 1
EN

Stack Overflow用户

发布于 2012-11-04 18:22:10

如何修改上面的类以符合我的条件?

你不能。身份证不见了。除非您没有至少与有问题的消息(即幕后发生的事件的消息)相关联的ID,否则您将无法创建一个具体的Class Swift_Plugins_DecoratorPlugin子类型,在请求替换时提供它自己的_Replacements接口来提供消息。

让我们创建您自己的插件,它仍然是替代插件的装饰器插件:

代码语言:javascript
运行
复制
<?php

interface My_Swift_Plugins_Decorator_Replacements extends Swift_Plugins_Decorator_Replacements
{
    public function setMessage(Swift_Mime_Message $message);
}

class My_Swift_Plugins_DecoratorPlugin extends Swift_Plugins_DecoratorPlugin implements My_Swift_Plugins_Decorator_Replacements
{

    private $_replacements;

    public function __construct(My_Swift_Plugins_Decorator_Replacements $replacements) {
        $this->_replacements = $replacements;
    }

    /**
     * Invoked immediately before the Message is sent.
     *
     * @param Swift_Events_SendEvent $evt
     */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt) {
        $this->setMessage($evt->getMessage());
        parent::beforeSendPerformed($evt);
    }

    public function setMessage(Swift_Mime_Message $message) {
        $this->_replacements->setMessage($message);
    }
}

如果您将ID分配给插件中的消息,您将在调用getReplacementsFor之前获得消息集。然后您可以将其分配给一个属性,并在该函数中读出它。

代码语言:javascript
运行
复制
class DbReplacements implements My_Swift_Plugins_Decorator_Replacements {
  private $message;
  public function setMessage(Swift_Mime_Message $message) {
      $this->message = $message;
  }

  public function getReplacementsFor($address) {
    $sql = sprintf(
      "SELECT * FROM user WHERE email = '%s' and id = '%d'",
      mysql_real_escape_string($address),
      $this->message->emailID;
    );

    $result = mysql_query($sql);

    if ($row = mysql_fetch_assoc($result)) {
      return array(
        '{username}'=>$row['username'],
        '{password}'=>$row['password']
      );
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13217217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档