1. Go to this page and download the library: Download vaibhavpandeyvpz/datum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vaibhavpandeyvpz / datum example snippets
use Databoss\Connection;
use Datum\Model;
// Create a databoss connection
$connection = new Connection([
Connection::OPT_DATABASE => 'mydb',
Connection::OPT_USERNAME => 'root',
Connection::OPT_PASSWORD => 'password',
]);
// Set the connection for all models
Model::connect($connection);
use Databoss\Connection;
use Datum\Model;
// Set a connection factory that will be called lazily when needed
Model::connect(function () {
return new Connection([
Connection::OPT_DATABASE => 'mydb',
Connection::OPT_USERNAME => 'root',
Connection::OPT_PASSWORD => 'password',
]);
});
// The connection will only be created when you first use a model
$user = User::find(1); // Connection is created here
use Datum\Model;
class User extends Model
{
protected static ?string $table = 'users';
protected static string $primaryKey = 'id';
/**
* Define attribute casts for automatic type conversion.
*
* @var array<string, string>
*/
protected static array $casts = [
'age' => 'int',
'created_at' => 'datetime',
'metadata' => 'array',
'is_active' => 'bool',
];
}
// Create
$user = new User([
'name' => 'John Doe',
'email' => '[email protected]',
]);
$user->save(); // created_at and updated_at are automatically set
// Read
$user = User::find(1);
$user = User::findOrFail(1); // Throws exception if not found
// Update
$user->name = 'Jane Doe';
$user->save(); // updated_at is automatically updated
// Delete
$user->delete();
// Get all
$users = User::all();
$user = new User(['name' => 'John', 'email' => '[email protected]']);
$user->save(); // created_at and updated_at are set automatically
// Later...
$user->name = 'Jane';
$user->save(); // updated_at is automatically updated, created_at remains unchanged
class User extends Model
{
protected static bool $timestamps = false;
}
class User extends Model
{
protected static string $createdAt = 'created_at';
protected static string $updatedAt = 'updated_at';
}
$user = new User([
'name' => 'John',
'email' => '[email protected]',
'created_at' => '2020-01-01 10:00:00',
'updated_at' => '2020-01-02 10:00:00',
]);
$user->save(); // Your custom timestamps are preserved
use Psr\Clock\ClockInterface;
use Datum\Model;
// Set a custom clock
Model::clock($yourClockInstance);
use Samay\FrozenClock;
use Datum\Model;
// Freeze time at a specific moment
$frozenTime = new \DateTimeImmutable('2024-01-15 10:30:00');
Model::clock(new FrozenClock($frozenTime));
$user = new User(['name' => 'Test']);
$user->save(); // Will use the frozen time for timestamps
// UUID is automatically generated on insert
$item = new Item(['name' => 'My Item']);
$item->save();
echo $item->uuid; // e.g., "550e8400-e29b-41d4-a716-446655440000"
// You can also manually set a UUID
$item = new Item([
'uuid' => '550e8400-e29b-41d4-a716-446655440000',
'name' => 'Custom UUID Item'
]);
$item->save();
// Find by UUID
$item = Item::find('550e8400-e29b-41d4-a716-446655440000');
class User extends Model
{
protected static array $casts = [
'age' => 'int',
'created_at' => 'datetime',
'metadata' => 'array',
'is_active' => 'bool',
];
}
// When loading from database
$user = User::find(1);
$user->created_at; // DateTime object
$user->metadata; // Array (decoded from JSON)
$user->age; // Integer
// When setting values
$user->created_at = new DateTime('2024-01-15');
$user->metadata = ['role' => 'admin'];
$user->age = 25;
$user->save(); // Values are automatically cast for storage
class User extends Model
{
public function profile()
{
return $this->one(Profile::class, 'user_id');
}
}
// Usage
$user = User::find(1);
$profile = $user->profile; // Automatically loaded
class User extends Model
{
public function posts()
{
return $this->many(Post::class, 'user_id');
}
}
// Usage
$user = User::find(1);
$posts = $user->posts; // Array of Post models
class Post extends Model
{
public function user()
{
return $this->owner(User::class, 'user_id');
}
}
// Usage
$post = Post::find(1);
$user = $post->user; // User model
class User extends Model
{
public function roles()
{
return $this->owners(
Role::class,
'user_roles', // pivot table
'user_id', // foreign pivot key
'role_id' // related pivot key
);
}
}
// Usage
$user = User::find(1);
$roles = $user->roles; // Array of Role models
use Psr\EventDispatcher\EventDispatcherInterface;
use Soochak\EventManager;
use Datum\Model;
// Set dispatcher directly
$dispatcher = new EventManager();
Model::dispatcher($dispatcher);
// Or use a factory for lazy loading
Model::dispatcher(function () {
return new EventManager();
});
// Clear dispatcher (operations will work without events)
Model::dispatcher(null);
use Datum\Events\Saving;
use Datum\Events\Created;
use Soochak\EventManager;
$dispatcher = new EventManager();
Model::dispatcher($dispatcher);
// Listen to saving event
$dispatcher->attach(Saving::class, function (Saving $event) {
$model = $event->model;
echo "Saving model: {$model->name}\n";
// You can stop propagation to abort the operation
// $event->stopPropagation();
});
// Listen to created event
$dispatcher->attach(Created::class, function (Created $event) {
$model = $event->model;
echo "Model created with ID: {$model->id}\n";
});
// Create a user
$user = new User(['name' => 'John', 'email' => '[email protected]']);
$user->save(); // Events will be fired
use Datum\Events\Saving;
$dispatcher->attach(Saving::class, function (Saving $event) {
$model = $event->model;
// Validate and stop if invalid
if (empty($model->email)) {
$event->stopPropagation();
return;
}
});
$user = new User(['name' => 'John']); // No email
$result = $user->save(); // Returns false, model not saved
use Soochak\EventManager;
use Datum\Events\Saving;
use Datum\Events\Created;
use Datum\Events\Updated;
use Datum\Events\Deleting;
use Datum\Model;
// Setup dispatcher
$dispatcher = new EventManager();
Model::dispatcher($dispatcher);
// Log all saves
$dispatcher->attach(Saving::class, function (Saving $event) {
$model = $event->model;
error_log("Saving: " . get_class($model) . " #{$model->id}");
});
// Log creates
$dispatcher->attach(Created::class, function (Created $event) {
$model = $event->model;
error_log("Created: " . get_class($model) . " #{$model->id}");
});
// Log updates
$dispatcher->attach(Updated::class, function (Updated $event) {
$model = $event->model;
error_log("Updated: " . get_class($model) . " #{$model->id}");
});
// Log deletes
$dispatcher->attach(Deleting::class, function (Deleting $event) {
$model = $event->model;
error_log("Deleting: " . get_class($model) . " #{$model->id}");
});
// Now all model operations will be logged
$user = new User(['name' => 'John', 'email' => '[email protected]']);
$user->save(); // Logs: Saving, Created, Saved
$user->name = 'Jane';
$user->save(); // Logs: Saving, Updating, Updated, Saved
$user->delete(); // Logs: Deleting, Deleted
use Psr\EventDispatcher\EventDispatcherInterface;
// In your service container or bootstrap
Model::dispatcher(function () use ($container) {
return $container->get(EventDispatcherInterface::class);
});