Need advice on mysql normalization and php class structure

I am trying to create a system that records download statistics (media, video, audio). so I came up with 3 tables, 1 for audio, 1 for video and 1 for photo. and here is the structure

+-----------+---------+------+-----+---------+----------------+
| 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    |                |
+-----------+---------+------+-----+---------+----------------+

all three tables have the same fields, since I think (so far) I need to distinguish medians for each and a specific table, this is an excess value

in any case, since each of the media is handled the same way, so I think I should only build the class once and use methods that depend on which media I access at that time. here is the class:

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 . ? ,

UPDATE

, , - :

  • (, , ..) .

, "media_id" , , INSERT, 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;
    }
}

, , ?

+3
2

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

1, 2, 3 , . WHERE, . SELECT * FROM maintable WHERE media_type_id = 3, .

+2

"media_type". , , .

+2

Source: https://habr.com/ru/post/1775834/


All Articles