nemirovskiy / medoo-repository
Base repository implementation for Medoo with declarative table schema, strict field/type validation and simple schema installation.
Requires
- php: >=8.2
- ext-mbstring: *
- catfan/medoo: ^2.1
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2026-05-11 05:44:55 UTC
README
Base repository implementation for Medoo that:
- Stores table schema (fields, types and options) in PHP arrays.
- Validates fields and types on create/update operations.
- Provides simple table installation (auto‑generated
CREATE TABLESQL from schema).
This library is useful when you want a lightweight, type‑checked repository layer on top of Medoo, without a full ORM.
Installation
Install via Composer:
composer require nemirovskiy/medoo-repository
Or, when developing locally, add the repository to your composer.json and require it.
Basic usage
1. Configure Medoo
use Medoo\Medoo; $database = new Medoo([ 'type' => 'mysql', 'host' => '127.0.0.1', 'database' => 'test', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', ]);
2. Extend BaseRepository
namespace App\Repository; use Medoo\Medoo; use Medoo\Repository\BaseRepository; final class UserRepository extends BaseRepository { public function __construct(Medoo $db) { parent::__construct($db); $this->table = 'users'; $this->schema = [ 'id' => [ 'type' => 'int', 'unsigned' => true, 'auto_increment' => true, ], 'email' => [ 'type' => 'string', 'length' => 255, 'nullable' => false, 'unique' => true, ], 'name' => [ 'type' => 'string', 'length' => 255, 'nullable' => false, ], 'age' => [ 'type' => 'int', 'unsigned' => true, 'nullable' => true, ], 'active' => [ 'type' => 'bool', 'nullable' => false, 'default' => 1, ], 'created_at' => [ 'type' => 'datetime', 'nullable' => false, 'default' => 'CURRENT_TIMESTAMP', ], ]; $this->fillable = ['email', 'name', 'age', 'active']; $this->required = ['email', 'name']; } }
3. Install table and use the repository
use App\Repository\UserRepository; $users = new UserRepository($database); // Create table if it does not exist $users->install(); // Create user (fields and types are validated) $id = $users->create([ 'email' => 'john.doe@example.com', 'name' => 'John Doe', 'age' => 30, ]); // Load user $user = $users->findById($id); // Update user $users->update($id, [ 'age' => 31, 'active' => false, ]); // Delete user // $users->delete($id);
Key features
-
Declarative schema
Define your table columns in a PHP array: type, length, nullable, default, unique, indexes,auto_increment, etc. -
Strict validation
Oncreateandupdate:- Only
fillablefields are accepted. - Required fields must be present.
- Types are strictly validated (
int,float,bool,string,datetime). - Values outside schema cause an exception.
- Only
-
Schema installation (MySQL only)
install()builds aCREATE TABLE IF NOT EXISTSSQL statement based on schema and executes it via Medoo.
Only for MySQL/MariaDB. For PostgreSQL, SQLite or other databases, do not callinstall()/uninstall()— create and drop tables via your own migrations or overridegenerateCreateTableSql().
Database support
- CRUD (
create,findById,update,delete) works with any database Medoo supports (MySQL, PostgreSQL, SQLite, etc.). - install() and uninstall() generate and run MySQL-only SQL. Use them only with MySQL/MariaDB; for other DBs use migrations or override the schema methods.
Examples
There are runnable examples in the examples/ directory:
examples/UserRepository.php– demo user repositoryexamples/demo.php– demo script that installs the table and performs basic CRUD
Run:
php examples/demo.php
Make sure your database configuration in demo.php matches your local environment.