pomm-project/model-manager
PHP model manager for Pomm: manage models, mapping and persistence for PostgreSQL in a clean, extensible way. Provides tools to define model classes, query and hydrate results, and integrate data access layers for robust domain-driven apps.
Installation Add the package via Composer:
composer require pomm-project/model-manager
Register the service provider in config/app.php under providers:
PommProject\ModelManager\ModelManagerServiceProvider::class,
Basic Configuration
Publish the config file (if needed) and set up your Pomm database connection in config/pomm.php:
'connections' => [
'default' => [
'driver' => 'pomm',
'host' => 'localhost',
'port' => 5432,
'database' => 'your_db',
'user' => 'your_user',
'password' => 'your_password',
],
],
First Use Case: Define a Model
Create a model class extending PommProject\ModelManager\Model:
use PommProject\ModelManager\Model;
class User extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $fillable = ['name', 'email'];
}
Use it in a controller or service:
$user = User::find(1);
$user->name = 'John Doe';
$user->save();
CRUD Operations Leverage Eloquent-like methods:
// Create
$user = new User(['name' => 'Alice', 'email' => 'alice@example.com']);
$user->save();
// Read
$user = User::find(1);
$users = User::all();
// Update
$user->email = 'new@example.com';
$user->save();
// Delete
$user->delete();
Query Building Use Pomm’s query builder via the model:
$activeUsers = User::where('active', true)->get();
$users = User::orderBy('name')->limit(10)->get();
Relationships Define relationships in model classes:
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Fetch relationships:
$post = Post::find(1);
$user = $post->user; // Lazy-loaded
Transactions Wrap operations in transactions:
DB::transaction(function () {
$user = new User(['name' => 'Bob']);
$user->save();
// Other operations...
});
PommProject\ModelManager\Schema to add custom table definitions or behaviors.creating, saved) for hooks:
User::creating(function ($model) {
$model->active = true;
});
$validator = Validator::make($user->toArray(), [
'email' => 'required|email',
]);
Primary Key Assumptions
The package assumes $primaryKey is set. If omitted, it defaults to id, but this may fail for non-standard schemas. Always explicitly define $primaryKey.
Pomm-Specific Quirks
Schema class or raw SQL for schema changes.config/pomm.php. Test it with:
$pomm = app('pomm');
$pomm->getConnection()->getDatabase()->getSchemaManager()->getTables();
Lazy Loading Overhead Relationships are lazy-loaded by default. Eager-load them to avoid N+1 queries:
$posts = Post::with('user')->get();
Mass Assignment Risks
Always define $fillable or $guarded to prevent mass assignment vulnerabilities.
Enable Pomm Logging
Add to config/pomm.php:
'log' => [
'enabled' => true,
'channel' => 'single',
],
Check logs in storage/logs/laravel.log.
Query Inspection
Use toSql() to debug queries:
$query = User::where('active', true);
dd($query->toSql());
Model Events
Override boot() to add custom logic:
protected static function boot()
{
static::saved(function ($model) {
Log::info("User saved: {$model->id}");
});
}
Custom Model Behavior
Override methods like save(), delete(), or toArray() for custom logic.
Query Scopes Add global scopes:
class User extends Model
{
protected static function booted()
{
static::addGlobalScope(new ActiveScope);
}
}
Pomm-Specific Extensions Access the underlying Pomm instance:
$pomm = $this->getPommConnection();
$result = $pomm->getConnection()->getDatabase()->execute('SELECT * FROM users');
How can I help you explore Laravel packages today?