ThinkPHP5.1ÖÐÔõôʵÏÖÔöɾ¸Ä²é²Ù×÷ÈÕÖ¾¼Í¼
Ò»¡¢ThinkPHP 5.1µÄÈÕÖ¾¼Í¼¹¦Ð§¼ò½é
ÔÚThinkPHP 5.1ÖУ¬ÈÕÖ¾¼Í¼¹¦Ð§ÒѾÄÚÖá£ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐʹÓÃËüÀ´¼Í¼²î±ðÀàÐ͵ÄÊÂÎñ£¬ÖîÈç¹ýʧÐÂÎÅ¡¢µ÷ÊÔÐÅÏ¢ºÍÓû§²Ù×÷µÈ¡£Ëĸö¼¶±ðµÄÈÕÖ¾¼Í¼ÓÉ¿ò¼ÜÌṩ£¬»®·ÖΪ£ºDEBUG¡¢INFO¡¢NOTICEºÍERROR¡£
³ýÁËÄÚÖõÄÈÕÖ¾¼¶±ðÍ⣬ÎÒÃÇ»¹¿ÉÒÔ½ç˵×Ô¼ºµÄÈÕÖ¾¼¶±ð¡£ÎÒÃÇ¿ÉÒÔ¾ÙÀý˵Ã÷£¬½¨ÉèÒ»¸öÃûΪ¡°CRUD¡±µÄÈÕÖ¾¼¶±ð£¬ÒԼͼÔöɾ¸Ä²é²Ù×÷¡£
ÔÚĬÈÏÇéÐÎÏ£¬ÈÕÖ¾½«¼Í¼µ½Ó¦ÓóÌÐò¸ùĿ¼ÏµÄlogĿ¼ÖС£ÈôÊÇÐèÒª¸üËûÈÕÖ¾´æ´¢Î»Ö㬿ÉÒÔͨ¹ýÐÞ¸ÄÉèÖÃÎļþÀ´ÊµÏÖ¡£ÉèÖÃÎļþÒ»Ñùƽ³£Î»ÓÚÓ¦ÓóÌÐòµÄconfigĿ¼ÖС£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢Ôöɾ¸Ä²é²Ù×÷ÈÕÖ¾¼Í¼ʵÏÖ
ÎÒÃÇÀ´¿´¿´ÔõÑùÔÚThinkPHP 5.1ÖÐʵÏÖÔöɾ¸Ä²é²Ù×÷µÄÈÕÖ¾¼Í¼¡£ÎÒÃǽ«Æ¾Ö¤ÒÔÏ°취ʵÏÖ£º
ÔÚconfigĿ¼Öн¨Éè×Ô½ç˵ÉèÖÃÎļþ
ΪÊý¾Ý±íÌí¼ÓÈÕÖ¾×Ö¶Î
ÔÚModelÖÐÖØдÔöɾ¸ÄÒªÁì
¼Í¼ÈÕÖ¾
ÔÚconfigĿ¼Öн¨Éè×Ô½ç˵ÉèÖÃÎļþ
ÎÒÃÇÐèÒªÏȽ¨ÉèÒ»¸ö×Ô½ç˵ÉèÖÃÎļþ£¬·ÅÔÚÓ¦ÓóÌÐòµÄconfigĿ¼Ï¡£ÎÒÃÇ°ÑÕâ¸öÎļþÃüÃûΪcommon_extra.php¡£
$config = [
'crud_log' => true, // ¼Í¼Ôöɾ¸Ä²éÈÕÖ¾ 'crud_ignore_fields' => ['create_time', 'update_time'] // ºöÂÔÈÕÖ¾¼Í¼µÄ×Ö¶Î
µÇ¼ºó¸´ÖÆ
];
ÔÚÉèÖÃÖУ¬ÎÒÃÇÉèÖÃÁËÁ½¸öÑ¡Ïî¡£µÚÒ»¸öÊÇcrud_log£¬ÓÃÓÚ¿ªÆô»ò¹Ø±ÕÈÕÖ¾¼Í¼¡£µÚ¶þ¸öÊÇcrud_ignore_fields£¬ÓÃÓÚÖ¸¶¨²»¼Í¼ÈÕÖ¾µÄ×ֶΡ£
ΪÊý¾Ý±íÌí¼ÓÈÕÖ¾×Ö¶Î
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪΪÊý¾Ý±íÌí¼ÓһЩ×Ö¶ÎÀ´¼Í¼Ôöɾ¸Ä²é²Ù×÷¡£ÎÒÃÇ¿ÉÒÔΪÿ¸öÊý¾Ý±íÌí¼ÓÒÔÏÂ×ֶΣº
id£¨×ÔÔöÖ÷¼ü£©
user_id£¨²Ù×÷Óû§id£¬¿ÉΪ¿Õ£©
action£¨²Ù×÷ÀàÐÍ£¬ÈçÔöÌí¡¢É¾³ý¡¢Ð޸ģ©
table_name£¨²Ù×÷µÄÊý¾Ý±íÃû£©
data£¨²Ù×÷µÄÊý¾Ý£©
created_at£¨²Ù×÷ʱ¼ä£©
ÔÚModelÖÐÖØдÔöɾ¸ÄÒªÁì
ÏÖÔÚÎÒÃÇÐèÒªÔÚModelÖÐÖØдÔöɾ¸ÄÒªÁ죬ÒÔʵÏÖÈÕÖ¾¼Í¼¡£ÎÒÃǽ«Ê¹ÓÃÈ«¾ÖÅÌÎÊ×÷ÓÃÓòÀ´ÊµÏÖËü¡£ÎÒÃǽ«ÖØдcreate¡¢updateºÍdeleteÒªÁì¡£
ÔÚÿ¸öÒªÁìÖУ¬ÎÒÃǽ«¼Í¼ÏìÓ¦µÄ²Ù×÷ÀàÐͺÍÊý¾Ý¡£È»ºó£¬ÎÒÃǽ«Í¨¹ýʹÓÃÈÕÖ¾ÀཫÈÕ־дÈëÈÕÖ¾Îļþ¡£
ÒÔÏÂÊÇһЩʾÀý´úÂ룺
namespace app\common\model;
use think\Model;
class User extends Model
{
protected $table = 'users'; // Ìí¼ÓÈ«¾ÖÅÌÎʹæÄ£ protected static function init() { // Ìí¼Ó²Ù×÷¼Í¼ static::beforeInsert(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'add'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); // Ð޸IJÙ×÷¼Í¼ static::beforeUpdate(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'update'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); // ɾ³ý²Ù×÷¼Í¼ static::beforeDelete(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'delete'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); }
µÇ¼ºó¸´ÖÆ
}
¼Í¼ÈÕÖ¾
×îºó£¬ÎÒÃǽ«¼Í¼ÈÕÖ¾¡£ÔÚÇ°ÃæµÄʾÀýÖУ¬ÎÒÃǽ«ÈÕ־дÈëÃûΪ¡°log¡±µÄÊý¾Ý±íÖС£¿ÉÊÇ£¬Äú¿ÉÒÔƾ֤ÐèÒª½«ÈÕ־дÈëÎļþ£¬·¢Ë͸øÈÕ־ЧÀÍÆ÷»ò½«Æä·¢Ë͵½ÆäËûµØ·½¡£
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÀÖ³ÉʵÏÖÁËÔÚThinkPHP 5.1ÖжÔÔöɾ¸Ä²é²Ù×÷¾ÙÐÐÈÕÖ¾¼Í¼µÄ¹¦Ð§¡£
ÒÔÉϾÍÊÇThinkPHP5.1ÖÐÔõôʵÏÖÔöɾ¸Ä²é²Ù×÷ÈÕÖ¾¼Í¼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡