需要有关mysql规范化和php类结构的建议
作者:互联网
我正在尝试建立一个记录日志,媒体(照片,视频,音频)上传统计信息的系统.所以我想出3张桌子,1张用于音频,1张用于视频,1张用于照片.这是结构
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| member_id | int(10) | NO | | NULL | |
| counter | int(11) | NO | | NULL | |
| daydate | text | NO | | NULL | |
| epochtime | text | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
这三个表都具有相同的字段,因为我认为(到目前为止)我需要区分每个表和特定表上的媒体,这是否多余?
无论如何,由于每种媒体都被视为相同,所以我认为我应该只建立一次类,并根据当时访问的媒体使用某种方法.这是课程:
require_once(INC_PATH.DS.'database.php');
class Log extends DatabaseObject {
protected static $db_fields = array('id', 'member_id', 'counter', 'daydate', 'epochtime');
public $id;
public $member_id;
public $counter;
public $daydate;
public $epochtime;
public function find_counter($table_name){
global $database;
$time = date('d-m-Y');
$timestamp = time();
$sql = "SELECT * FROM ". $table_name;
$sql .= " WHERE daydate = '".$this->daydate."'";
$sql .= " AND member_id = '".$this->member_id."'";
return self::find_by_sql($sql);
}
public function add_log($table_name){
global $database;
$tes = $this->find_counter();
if(!empty($tes)){
$sql = "UPDATE ".$table_name;
$sql .= " SET counter = counter+1";
$sql .= " WHERE daydate = '".$this->daydate."'";
$sql .= " AND member_id = '".$this->member_id."'";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}else{
$sql = "INSERT INTO ".$table_name;
$sql .= " (id, member_id, user_privelege, counter, daydate, epochtime)";
$sql .= " VALUES ('', '".$this->member_id."'";
$sql .= " , '".$this->user_privelege."', '1', '".$this->daydate."', '".$this->epochtime;
$sql .= "')";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}
}
}
到目前为止,这几乎可以正常工作,但是,我仍然对将表拆分为3个表的方式以及使用$table_name作为参数的方式仍存疑问.任何更好的建议?我真的很想改善我的代码,非常感谢
问候
更新
好了,根据大家的回答,我想澄清一下:
>我只需要记录上传的媒体
>我已经有了另一个单独的表来记录每种媒体的其他信息(长度,文件名,标题等).
可以说我添加了一个名为“ media_id”的字段并将表合并为一个字段,由于我添加和更新行的方式基于日期,因此如果找不到给定的日期和member_id,它将插入,否则它将更新,我想我也应该将add_log()方法的工作方式更改为:
public function add_log($table_name, $media_id){
global $database;
$tes = $this->find_counter();
if(!empty($tes)){
$sql = "UPDATE ".$table_name;
$sql .= " SET counter = counter+1";
$sql .= " WHERE daydate = '".$this->daydate."'";
$sql .= " AND member_id = '".$this->member_id."'";
$sql .= " AND media_id = '".$media_id."'";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}else{
$sql = "INSERT INTO ".$table_name;
$sql .= " (id, member_id, media_id, counter, daydate, epochtime)";
$sql .= " VALUES ('', '".$this->member_id."'";
$sql .= " , '".$media_id."', '1', '".$this->daydate."', '".$this->epochtime;
$sql .= "')";
$database->query($sql);
return ($database->affected_rows() == 1) ? true : false;
}
}
你们有什么感想 ??
再次感谢
解决方法:
将它们全部放在一个表中,并具有一个media_type列.
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| member_id | int(10) | NO | | NULL | |
| counter | int(11) | NO | | NULL | |
| daydate | text | NO | | NULL | |
| epochtime | text | NO | | NULL | |
| media_type| int | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
创建与另一个定义media_type的表的外键关系.
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| media_type_id| int | NO | PRI | NULL | |
| description | text | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
这将保存media_type_id(例如1)和media_type_description(例如音频).
例如
1, audio
2, video
3, photo
然后,主表将仅在每一行上包含ID 1、2、3,以表示它是哪种媒体类型.然后,您可以在应用程序中使用此功能通过WHERE子句进行约束,即您所指的是哪种媒体.例如从主表中选择* WHERE media_type_id = 3即可获取照片.
标签:normalization,class-design,mysql,php,database 来源: https://codeday.me/bug/20191105/1996734.html