सामग्री पर जाएं
TubePress — मुफ़्त, स्व-होस्टेड & सक्रिय रूप से अनुरक्षित
डेवलपर संदर्भ

मॉडल और डेटाबेस

TubePress डेटा लेयर: मॉडल क्लासेस, PDO डेटाबेस हेल्पर, प्रिपेयर्ड स्टेटमेंट, डेटाबेस स्कीमा और वर्शन्ड SQL माइग्रेशन समझाए गए।

TubePress दो पतली परतों के माध्यम से MySQL/MariaDB से संवाद करता है: models (प्रत्येक entity के लिए एक static class) और Database helper (PDO के ऊपर एक छोटा wrapper)। सीखने के लिए कोई ORM नहीं और कोई query builder नहीं — केवल prepared statements और अनुमानित method नाम।

Model परत

प्रत्येक core entity का src/Models/ में एक model है जो static methods को उजागर करता है। उपलब्ध models हैं Video, Category, Tag, Performer, Channel, Comment, Page, User, Setting, Favorite, History, Report, ContactMessage, MenuConfig और FooterConfig

$video  = Video::find($id);            // one row (or null)
$latest = Video::all(['status' => 'published', 'limit' => 20]);
$id     = Video::create($data);        // returns new id
Video::update($id, ['title' => 'New title']);
Video::delete($id);

Models अपनी entity के लिए विशिष्ट joins और filters को encapsulate करते हैं — उदाहरण के लिए Video::find() video को उसकी categories, tags, performers और channels के साथ वापस करता है, इसलिए controllers और templates सरल रहते हैं।

Database helper

किसी भी चीज़ के लिए जो model पहले से कवर नहीं करता, Database helper को सीधे उपयोग करें। प्रत्येक method prepared statements का उपयोग करती है — कभी भी user input को SQL में concatenate न करें।

Methodलौटाता हैउद्देश्य
Database::fetchOne($sql, $params)array\|nullपहली मिलती-जुलती row।
Database::fetchAll($sql, $params)arrayसभी मिलती-जुलती rows।
Database::insert($table, $data)intएक associative array insert करता है; नया id लौटाता है।
Database::update($table, $data, $where, $whereParams)intUpdate करता है; प्रभावित rows लौटाता है।
Database::delete($table, $where, $params)intDelete करता है; प्रभावित rows लौटाता है।
Database::count($table, $where, $params)intRow की संख्या।
Database::query($sql, $params)PDOStatementकोई भी statement, पूर्ण नियंत्रण के लिए।
Database::transaction($callback)mixedएक transaction में closure चलाता है (auto commit/rollback)।
$rows = Database::fetchAll(
    "SELECT * FROM videos WHERE status = ? ORDER BY id DESC LIMIT ?",
    ['published', 20]
);

$newId = Database::insert('categories', [
    'name' => 'Amateur',
    'slug' => 'amateur',
]);
लचीले connections। Helper utf8mb4, वास्तविक prepared statements (कोई emulation नहीं) और exception error mode का उपयोग करता है, और एक वास्तविक "server has gone away" error पर यह स्वचालित रूप से एक बार reconnect करता है — इसलिए लंबे समय तक चलने वाले workers एक idle MySQL timeout से बच जाते हैं।

Transactions

Multi-step writes को wrap करें ताकि सभी सफल हों या सभी roll back करें:

Database::transaction(function () use ($videoId, $catIds) {
    Database::delete('video_categories', 'video_id = ?', [$videoId]);
    foreach ($catIds as $cid) {
        Database::insert('video_categories', [
            'video_id' => $videoId, 'category_id' => $cid,
        ]);
    }
});

Schema

Database एक स्वच्छ, normalised MySQL schema का उपयोग करता है। Core tables में users, settings, categories, tags, performers, channels, videos और join tables video_categories, video_tags, video_performers — साथ ही comments, user_favorites, user_history, video_likes, pages, plugins, themes और migrations शामिल हैं। वैकल्पिक subsystems अपनी tables जोड़ते हैं (import jobs, media storages, transcode jobs/servers, ad zones/spots, reports, feed sources, translation tables और अधिक) जब आप उन्हें सक्षम करते हैं।

Migrations

Schema changes sql/ में क्रमांकित files के रूप में रहते हैं — 001_users.sql, 002_settings.sql, … — installation के दौरान और प्रत्येक update के बाद Migrator::run() द्वारा क्रम में लागू किए जाते हैं। प्रत्येक migration additive है और migrations table में दर्ज किया जाता है ताकि यह कभी दो बार न चले।

  • Plugin से schema को extend करने के लिए, अपने CREATE TABLE statements भेजें और उन्हें plugin की install() method में चलाएं (देखें Hooks & plugins)।
  • कभी भी applied migration को पुनः क्रमांकित या संपादित न करें — एक नई, उच्च-क्रमांकित migration जोड़ें।

Model लिखना

Model केवल एक static class है जो एक table के लिए Database helper को wrap करती है:

class Promo
{
    public static function current(): ?array
    {
        return Database::fetchOne(
            "SELECT * FROM promos WHERE active = 1
             ORDER BY id DESC LIMIT 1"
        );
    }
}

अगले चरण

अभी भी अटके हैं?

अपने डैशबोर्ड से टिकट खोलें और हमारी टीम आपकी मदद करेगी।

लाइव डेमो आज़माएं → TubePress डाउनलोड करें →