3

在 Drupal 中,我首先将出现在私人消息正文中的电子邮件序列化并存储在 MySQL 中,如下所示:

function prvtmsg_list($body) {
  $notify = array();
  if (isset($body->emails)) {
    $notify['mid'] = $body->mid;
    $notify['emails'] = serialize($body->emails);
  }
  if (isset($body->vulgar_words) {
    $notify['mid'] = $body->mid;
    $notify['vulgar_words'] = serialize($message->vulgar_words);
  }
  if (isset($notify['mid'])) {
    drupal_write_record('prvtmsg_notify', $notify);
  }
}

当我稍后尝试检索它们时,电子邮件用户化失败,我像这样检索它们:

function prvtmsg_list_notify() {
  // Select fields from prvtmsg_notify and Drupal pm_message tables
  $query = db_select('prvtmsg_notify', 'n');
  $query->leftJoin('pm_message', 'm', 'n.mid = m.mid');
  $query->fields('n', array('mid', 'emails', 'vulgar_words'));
  $query->fields('m', array('mid', 'author', 'subject', 'body', 'timestamp'));
  orderBy('timestamp', 'DESC');
  $query = $query->extend('PagerDefault')->limit(20);
  $result = $query->execute()->fetchAll();

  $rows = array();
  foreach ($result as $notify) {
    $rows[] = array(
      $notify->author,
      $notify->subject,
      implode(', ', unserialize($notify->emails)),
      implode(', ', unserialize($notify->vulgar_words)),
    );
  }

  $build = array();
  $build['table'] = array(
    '#theme' => 'table',
    '#header' => array(
      t('Author'),
      t('Message subject'),
      t('Emails captured'),
      t('Vulgar Words Captured'),
    ),
    '#rows' => $rows,
  );
  $build['pager']['#theme'] = 'pager';

  return $build;

}

也许我序列化电子邮件的方式是错误的?因为:

dpm(unserialize($notify->emails);

给出 Array, Array, Array - 这意味着:

Array( [0] => Array() [1] => Array() [2] => Array() [3] => Array() )

令人惊讶的是,未序列化的粗俗单词显示正常!我不确定是否可以像这样序列化电子邮件:

$notify['emails'] = serialize (array($body->emails));

过去我遇到了反序列化对我不起作用的确切情况,我有一些不清楚的地方,我需要学习它。谁能确认或告诉我出了什么问题?

注意以上代码来自内存,可能不准确,因为我目前无权访问它。

4

1 回答 1

1

如果我正确地阅读了这个,你正在将一个数组写入一个数据库

drupal_write_record('prvtmsg_notify', $notify);

应该:

drupal_write_record('prvtmsg_notify', serialize($notify));

你很可能不再需要

$notify['emails'] = serialize($body->emails);

而是可以写:

$notify['emails'] = $body->emails;

从数据库中检索它后,您可以反序列化数组并对其进行迭代:

$array = unserialize(someFunctionToGetPrvtmsg_notifyFromTheDb());
//the array should be the same as the one you serialized
于 2013-09-28T22:16:19.860 回答