id = $id; $this->title = $title; $this->description = $description; $this->author_id = $author_id; $this->likes = $likes; $this->dislikes = $dislikes; } /** * Creates the table for the object type in the database. */ public static function CreateTable(): void { throw new SecurityFaultException("Attempt to create table on video object."); } /** * Drops the table for the object type from the database. */ public static function DropTable(): void { throw new SecurityFaultException("Attempt to drop table on video object."); } /** * Saves the object to the database. */ public function Save() { Hajeebtok::$Database->Query("INSERT INTO videos (title, description, author_id, likes, dislikes) VALUES (:title, :description, :author_id, :likes, :dislikes)", [ "title" => $this->title, "description" => $this->description, "author_id" => $this->author_id, "likes" => $this->likes, "dislikes" => $this->dislikes ]); $id = Hajeebtok::$Database->LastInsertId(); Logger::Debug("Saved video id ($id)."); } /** * Deletes the object from the database. */ public function Delete() { Hajeebtok::$Database->Query("DELETE FROM videos WHERE id = :id", ["id" => $this->id]); } /** * Loads the object from the database. */ public function Load() { if($this->id === null) throw new VideoNotFoundException(0, 404); $data = Hajeebtok::$Database->Row("SELECT * FROM videos WHERE id = :id", ["id" => $this->id]); if(empty($data)) throw new VideoNotFoundException($this->id, 404); Logger::Debug("$data"); $this->title = $data["title"]; $this->description = $data["description"]; $this->author_id = $data["author_id"]; $this->likes = $data["likes"]; $this->dislikes = $data["dislikes"]; $this->comments = Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $this->id]); $this->shares = Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $this->id]); } public function LoadMany(): array { if(empty($this->title)) throw new VideoNotFoundException(0, 404); $videos = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE title LIKE :title", ["title" => "%$this->title%"]); if(empty($videos)) throw new VideoNotFoundException(0, 404); return $videos; } }