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 に答える