0

このシナリオを手に入れたとき、私は質問を受けていました:

ユーザーが何をしたかについて履歴ログを作成する必要があり、もちろんユーザーはさまざまなアクションを実行できます。

私はそれを作るための2つの異なる2つの方法を考えました.正しい方法に従うのを手伝ってくれる人が必要です.

最初の方法:

2 つの異なるテーブルを作成する

  • History_user
  • History_type

History_userテーブル

id | user_id | history_type (int)
 1      1             1
 1      3             2

History_type

id | name_action (string)
 1   The user has posted on the wall
 2   The user has change his profile picture

そして、クエリに参加するだけですHistory_user.history_type = History_type.id

2 番目の方法:

History_user テーブルと Converter というヘルパー サンプルを作成します。

<?php

class Converter {

  function history($type_history) {
        switch($type_history) {
           case 1:
            $human_history = "The user has posted on the wall";
           break;

           case 2:
             $human_history = "The user has change his profile picture";
           break;
        }

        return $human_history;
  }

}

$converter = new Converter();
$converter->history(1);

パフォーマンスと保守性の観点から、それを行うためのより良い方法を探していました。ありがとうございました。

4

1 に答える 1

1

情報表現には、ヘルパーと History_type テーブルの両方が必要です。ユーザーの操作で 1 つのテーブルにのみ挿入するため、パフォーマンスに関してはそれほど重要ではありません。データを表現する必要がある場合は、アクションの説明を取得するために、もう 1 つのクエリが必要になります (パフォーマンスが必要な場合は、結合なし、ofc)。したがって、2 テーブルの方法はより柔軟で拡張可能です。

静的キャッシュ変数を持つヘルパー関数を実行することもできます-idの配列=>アクションの名前。これは、次のようにhistory()関数に遅延ロードされます。

class Converter {
    protected static $_cache;

    protected static function _loadCache() {
        if (null !== self::$_cache) {
            return;
        }
        self::$_cache = array();
        $query = "SELECT * FROM `History_type`";
        $res = mysql_query($query);
        while ($row = mysql_fetch_assoc($res)) {
            self::$_cache[(int) $row['id']] = $row['action'];
        }
    }

    public static function history($id) {
        self::_loadCache();
        return isset(self::$_cache[$id]) ? self::$_cache[$id] : 'Undefined action';
    }
}

Converter::history(1);
于 2014-01-28T11:34:00.833 に答える