ccmbenchmark/ting
Ting is a lightweight Laravel/PHP package from ccmbenchmark that adds simple benchmarking utilities to time and compare code paths. Use it to measure execution duration, collect results, and quickly spot slow sections during local development and profiling.
Installation
composer require ccmbenchmark/ting
Add the service provider to config/app.php under providers:
Ccmbenchmark\Ting\TingServiceProvider::class,
Define a Model
Create a model extending Ccmbenchmark\Ting\Datamapper:
namespace App\Models;
use Ccmbenchmark\Ting\Datamapper;
class User extends Datamapper
{
protected $table = 'users';
protected $primaryKey = 'id';
}
First Use Case: Fetching a Record
$user = User::find(1);
// Automatically maps DB row to object properties
Configuration
Check config/ting.php for default settings (e.g., connection, caching). Override via:
'default' => [
'connection' => 'mysql',
'cache' => true,
],
CRUD Operations
// Create
$user = new User(['name' => 'John', 'email' => 'john@example.com']);
$user->save();
// Read
$users = User::all(); // Collection of models
$user = User::find(1);
// Update
$user->name = 'Jane';
$user->save();
// Delete
$user->delete();
Query Building Use fluent methods (similar to Eloquent):
$activeUsers = User::where('active', true)->orderBy('name')->get();
Relationships (Basic)
Define relationships via hasOne, hasMany:
class Post extends Datamapper
{
public function user()
{
return $this->hasOne(User::class, 'user_id');
}
}
Events & Hooks Override lifecycle methods:
protected static function boot()
{
static::creating(function ($model) {
$model->created_at = now();
});
}
Datamapper alongside Eloquent traits for mixed workflows.FormRequest or Validator before save().DB::transaction() for atomicity.cache: true in config to reduce DB load for repeated queries.No Built-in Relationship Loading
Unlike Eloquent, ting doesn’t auto-load relationships. Use with() explicitly:
$user = User::with('posts')->find(1);
Primary Key Assumptions
Assumes $primaryKey is set correctly. Defaults to id, but override if using custom keys.
Mass Assignment Risks
No $guarded or $fillable by default. Validate input manually or use traits:
use Ccmbenchmark\Ting\Concerns\HasFillable;
Connection Overrides Config must specify the DB connection. Defaults to Laravel’s default connection.
config/database.php) to inspect raw SQL.static::saved() or static::deleted() for debugging lifecycle hooks.php artisan config:clear) if changes to ting.php aren’t reflected.Custom Query Builder
Override newQuery() to inject logic:
protected function newQuery()
{
return parent::newQuery()->where('deleted_at', null);
}
Dynamic Attributes
Use getAttribute() and setAttribute() for computed properties:
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
Repository Pattern
Create a repository class to abstract ting usage:
class UserRepository {
public function findActiveUsers()
{
return User::where('active', true)->get();
}
}
Testing
Use Mockery to stub Datamapper queries in unit tests:
$mockUser = Mockery::mock(User::class);
$mockUser->shouldReceive('find')->andReturn(new User());
How can I help you explore Laravel packages today?