maatify/db-model-v3

Official PHP library for maatify.dev MySql Database Model handler, known by our team

1.2.0015 2024-09-19 13:26 UTC

This package is auto-updated.

Last update: 2024-10-19 13:45:01 UTC


README

Current version Packagist PHP Version Support Monthly Downloads Total Downloads Stars

DB-Model

maatify.dev MySql Database PDO Model handler, known by our team

Installation

composer require maatify/db-model-v3

Usage

Create DbConnector Connection Class Extends Model

<?php

namespace Maatify\files;

use Maatify\Json\Json;
use Maatify\Logger\Logger;
use Maatify\Model\DB;
use Maatify\Model\Model;
use PDOException;

abstract class DbConnector extends Model
{
    private static DB $connection;

    public function __construct()
    {
        if(empty(static::$connection)){
            try {
                static::$connection = new DB([
                    'host'     => __DB_HOST__,
                    'user'     => __DB_USER__,
                    'password' => __DB_PASSa__,
                    'dbname'   => __DB_DATABASE__,
                    'charset'  => 'utf8mb4',
                ]);
            }
            catch (PDOException $e){
                Logger::RecordLog([$e->getMessage(), (int)$e->getCode()], 'app_connections');
                Json::DbError(__LINE__);
            }
        }
        parent::__construct();
        $this->db = static::$connection;
    }
}

Create Connection Table Handler Class Extends DbConnector

<?php

class Info extends DbConnector
{
    const TABLE_NAME = 'info';
    protected string $tableName = self::TABLE_NAME;
    const IDENTIFY_TABLE_ID_COL_NAME = 'id';
    protected string $identify_table_id_col_name = self::IDENTIFY_TABLE_ID_COL_NAME;

    public function Insert(int $id, string $name): void
    {
        $this->Add(
            [
                $this->identify_table_id_col_name   => $id,
                'name' => $name,
            ]
        );
    }

    public function SelectAll(): array
    {
        return $this->Rows($this->tableName);
    }

    public function SelectById(int $id): array
    {
        return $this->Rows($this->tableName, '*', '`id` = ?', [$id]);
    }

    public function UpdateNameByID(int $id, string $name): bool
    {
        return $this->Edit(['name'=>$name], '`id` = ?', [$id]);
    }
}