model / db
MySQL abstraction layer for ModEl Framework
Installs: 3 746
Dependents: 2
Suggesters: 3
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- ext-pdo: *
- model/cache: ^0.5.0
- model/config: ^0.7.0
- model/db-parser: ^0.2.0
- model/events: ^0.2.0
- model/providers-finder: ^0.3.0
- model/query-builder: ^0.6.0
- robmorgan/phinx: ^0.16.0
- dev-master
- v0.9.13
- v0.9.12
- v0.9.11
- v0.9.10
- v0.9.9
- v0.9.8
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.17
- v0.7.16
- v0.7.15
- v0.7.14
- v0.7.13
- v0.7.12
- v0.7.11
- v0.7.10
- v0.7.9
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.12
- v0.6.11
- v0.6.10
- v0.6.9
- v0.6.8
- v0.6.7
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.24
- v0.5.23
- v0.5.22
- v0.5.21
- v0.5.20
- v0.5.19
- v0.5.18
- v0.5.17
- v0.5.16
- v0.5.15
- v0.5.14
- v0.5.13
- v0.5.12
- v0.5.11
- v0.5.10
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2025-09-30 12:59:54 UTC
README
A MySQL abstraction layer for the ModEl Framework, providing elegant database operations with built-in caching, transactions, migrations, and event dispatching.
Features
- Simple CRUD Operations: Intuitive methods for insert, update, delete, and select operations (uses
model/query-builder
under the hood) - Smart Caching: Automatic caching for frequently accessed data with intelligent invalidation
- Transaction Management: Nested transaction support with automatic commit/rollback
- Database Migrations: Integrated Phinx-based migration system
- Event System: Comprehensive event dispatching for all database operations
- Provider System: Extensible provider architecture for customizing database behavior
- Query Limits: Built-in protection against runaway queries
- Type Safety: Automatic type casting for database values
Installation
composer require model/db
Configuration
The package uses the model/config
system. Configuration is stored under the db
key:
[ 'databases' => [ 'primary' => [ 'host' => 'localhost', 'port' => 3306, 'username' => 'root', 'password' => '', 'name' => 'database', 'charset' => 'utf8', 'migrations' => ['migrations/'], // Migration paths 'cache_tables' => [], // Tables to always cache 'limits' => [ 'query' => 100, // Max same query executions 'table' => 10000, // Max queries per table 'total' => null, // Max total queries ], ], ], ]
Basic Usage
Getting a Connection
use Model\Db\Db; // Get the default connection (first defined or 'primary') $db = Db::getConnection(); // Get a specific connection $db = Db::getConnection('alternative_db');
Query builder integration
This package leans on the model/query-builder
package for the query building.
Here follows a quick overview, but refer to said package for more details on "where" clauses building and options.
Insert Operations
// Insert a single row $id = $db->insert('users', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30 ]);
Select Operations
// Select by ID $user = $db->select('users', 1); // Select with conditions $user = $db->select('users', ['email' => 'john@example.com']); // Select all rows $users = $db->selectAll('users'); // Streaming results (it returns a generator and not an array) $users = $db->selectAll('users', [], ['stream' => true]); foreach ($users as $user) { // Process each user }
Streaming is the default behavior, but you can disable it by setting the stream
option to false
.
Update Operations
// Update by ID $db->update('users', 1, ['name' => 'Jane Doe']); // Update entire table (requires confirmation) $db->update('users', [], ['active' => true], ['confirm' => true]); // Update or insert $id = $db->updateOrInsert('users', ['email' => 'john@example.com'], [ 'name' => 'John Updated', 'age' => 31 ]);
Delete Operations
// Delete by ID $db->delete('users', 1); // Delete with conditions $db->delete('users', ['status' => 'inactive']); // Delete entire table (requires confirmation) $db->delete('users', [], ['confirm' => true]);
Count Operations
// Count all rows $total = $db->count('users'); // Count with conditions $active = $db->count('users', ['status' => 'active']); // Count with grouping $count = $db->count('users', [], ['group_by' => 'status']);
Advanced Features
Transactions
$db->beginTransaction(); try { $id = $db->insert('users', ['name' => 'John']); $db->insert('profiles', ['user_id' => $id]); $db->commit(); } catch (\Exception $e) { $db->rollBack(); throw $e; }
Transactions support nesting - only the outermost transaction actually commits to the database.
Bulk/Deferred Inserts
For high-performance bulk operations, use deferred inserts:
// Defer inserts and flush every 100 rows for ($i = 0; $i < 1000; $i++) { $db->insert('users', [ 'name' => 'User ' . $i, 'email' => 'user' . $i . '@example.com', ], ['defer' => 100]); } // Manually flush remaining inserts $db->bulkInsert('users');
Caching
The package provides intelligent caching:
- Simple queries on small tables (< 200 rows) are automatically table-cached (meaning that the whole table is cached in memory)
- Tables listed in
cache_tables
config are always cached - Cache is automatically invalidated on INSERT, UPDATE, or DELETE
- In-memory cache for repeated queries in the same request (with the same parameters)
// Disable cache for a query $users = $db->selectAll('users', [], ['cache' => false]); // Manually invalidate cache for a table $db->changedTable('users');
Union Queries
$results = $db->unionSelect([ [ 'table' => 'active_users', 'where' => ['status' => 'active'], ], [ 'table' => 'pending_users', 'where' => ['status' => 'pending'], ], ]);
Raw Queries
$statement = $db->query('SELECT * FROM users WHERE age > 18'); foreach ($statement as $row) { // Process row }
Database Introspection
// Get table structure $table = $db->getTable('users'); echo $table->name; print_r($table->columns); print_r($table->primary); print_r($table->indexes); // Get the parser $parser = $db->getParser(); // Get the query builder $builder = $db->getBuilder(); // Get the PDO instance $pdo = $db->getDb();
Events
The package dispatches events for all database operations:
Model\Db\Events\Query
- Any query executionModel\Db\Events\InsertQuery
- Before insertModel\Db\Events\InsertedQuery
- After insertModel\Db\Events\UpdateQuery
- Before updateModel\Db\Events\DeleteQuery
- Before deleteModel\Db\Events\SelectQuery
- Before selectModel\Db\Events\ChangedTable
- Table data changed
See model/events
package for more details.
Migrations
The package uses Phinx for database migrations.
Running Migrations
Migrations are automatically run when refreshing the ModEl Framework cache. To manually run migrations:
use Model\Db\Db; // Run all migrations for all databases Db::migrate(); // Run migrations for a specific database Db::migrate('primary');
Creating Migrations
Create migration files in the configured migrations folder:
<?php use Phinx\Migration\AbstractMigration; class CreateUsersTable extends AbstractMigration { public function change() { $table = $this->table('users'); $table->addColumn('name', 'string', ['limit' => 100]) ->addColumn('email', 'string', ['limit' => 100]) ->addColumn('age', 'integer') ->addColumn('created_at', 'datetime') ->addIndex(['email'], ['unique' => true]) ->create(); } }
See robmorgan/phinx
package docs for more details.