0

当我遇到这种情况时,我遇到了一个问题:

我必须记录用户所做的事情,当然用户可以做很多不同的操作。

我认为有两种不同的方法来实现它我只需要有人可以帮助我遵循正确的方法。

第一种方式:

创建 2 个不同的表

  • 历史用户
  • 历史类型

History_user

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

历史类型

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

第二种方式:

是创建 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

helper 和 History_type 表都是信息表示所必需的。就性能而言,这并不重要,因为您只会在一个表中插入用户操作。如果你需要表示数据,你只需要一个查询来获取操作的描述(如果你想要一些性能,没有连接,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 回答