[■]
codesaur/dataobject
1=[↕]
1:1 ══ composer require codesaur/dataobject
codesaur/dataobject · v10.2.1
DataObject
PDO-based data model and table management component (MySQL / PostgreSQL / SQLite)
[■]
DataObject
The core data layer component of the codesaur ecosystem. Declare columns on a Model class and the table is created automatically on first use; insert / update / select / delete run with the same code on MySQL, PostgreSQL and SQLite. LocalizedModel stores multi-language content in a separate {table}_content table. Where user input reaches an identifier position that cannot be parameterized (ORDER BY, GROUP BY), the model acts as its own whitelist (assertColumn, orderBy).
Features
- Model - base class for models targeting a single table
- LocalizedModel - multi-language content stored in {table}_content
- Column - type, length, default, primary, unique, auto increment
- Automatic table creation on first use with an __initial() hook
- MySQL / PostgreSQL / SQLite with one codebase - Constants::DRIVER_*
- getById, getRowWhere, getRows, countRows, insert, updateById, deleteById
- assertColumn / assertDirection / quoteIdentifier / orderBy - whitelist for user input in ORDER BY and GROUP BY (SQL injection guard)
- PDOTrait, TableTrait - reusable PDO / table operations
Core classes
| Class | Purpose |
|---|---|
Model | Base class for models targeting a single table |
LocalizedModel | Base class for models storing content in multiple languages - orderBy() returns the column with its JOIN alias |
Column | Defines table column structure |
Constants | Centralized drivers, error codes, column name constants |
PDOTrait | Trait centralizing PDO operations |
TableTrait | Trait with basic table operations and the column whitelist helpers |
Quick example
use codesaur\DataObject\Model;
use codesaur\DataObject\Column;
class UserModel extends Model
{
public function __construct(\PDO $pdo)
{
$this->setInstance($pdo);
$this->setColumns([
(new Column('id', 'bigint'))->primary(),
(new Column('username', 'varchar', 64))->unique(),
new Column('password', 'varchar', 255),
(new Column('is_active', 'tinyint'))->default(1),
new Column('created_at', 'datetime'),
]);
$this->setTable('users');
}
}
$pdo = new \PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$userModel = new UserModel($pdo);
// Нэмэх / Insert
$user = $userModel->insert([
'username' => 'john',
'password' => password_hash('secret', PASSWORD_DEFAULT),
'created_at' => date('Y-m-d H:i:s'),
]);
// Унших / Read
$user = $userModel->getById(1);
$user = $userModel->getRowWhere(['username' => 'john']);
$total = $userModel->countRows(['WHERE' => 'is_active=1']);
// Эрэмбэлэлт: хэрэглэгчийн оролт баганын цагаан жагсаалтаар шүүгдэнэ
// Ordering: user input is filtered through the model column whitelist
$rows = $userModel->getRows([
'ORDER BY' => $userModel->orderBy($_GET['sort'] ?? 'id', $_GET['dir'] ?? 'DESC')
]);
Running tests
composer test
[■]
Info
Install
composer require codesaur/dataobject
Requirements
- PHP 8.2.1+
- ext-pdo
- MySQL / PostgreSQL / SQLite
- Composer
Documentation