1. Go to this page and download the library: Download neuron-php/orm 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/ */
neuron-php / orm example snippets
use Neuron\Orm\Model;
// Set the PDO connection for all models
Model::setPdo($pdo);
use Neuron\Orm\Model;
use Neuron\Orm\Attributes\{Table, BelongsTo, BelongsToMany};
#[Table('posts')]
class Post extends Model
{
private ?int $_id = null;
private string $_title;
private string $_body;
private int $_authorId;
#[BelongsTo(User::class, foreignKey: 'author_id')]
private ?User $_author = null;
#[BelongsToMany(Category::class, pivotTable: 'post_categories')]
private array $_categories = [];
#[BelongsToMany(Tag::class, pivotTable: 'post_tags')]
private array $_tags = [];
// Implement fromArray() method
public static function fromArray(array $data): static
{
$post = new self();
$post->_id = $data['id'] ?? null;
$post->_title = $data['title'] ?? '';
$post->_body = $data['body'] ?? '';
$post->_authorId = $data['author_id'] ?? 0;
return $post;
}
// Getters and setters...
}
// Begin a transaction
Model::beginTransaction();
// Commit the transaction
Model::commit();
// Rollback the transaction
Model::rollBack();
// Check if in transaction
$inTransaction = Model::inTransaction();
// Execute callback in transaction (auto commit/rollback)
$result = Model::transaction(function() {
// Your database operations
return $someValue;
});
// Without eager loading (N+1 problem)
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // Triggers a query for each post
}
// With eager loading (2 queries total)
$posts = Post::with('author')->all();
foreach ($posts as $post) {
echo $post->author->name; // No additional queries
}
// Multiple relations
$posts = Post::with(['author', 'categories', 'tags'])->all();
// Using create() - creates and saves in one step
$user = User::create([
'username' => 'john',
'email' => '[email protected]'
]);
// Using save() on a new instance
$user = new User();
$user->setUsername('jane');
$user->setEmail('[email protected]');
$user->save();
// Using fromArray() and save()
$user = User::fromArray([
'username' => 'bob',
'email' => '[email protected]'
]);
$user->save();
// Using update() method
$user = User::find(1);
$user->update([
'email' => '[email protected]'
]);
// Using setters and save()
$user = User::find(1);
$user->setEmail('[email protected]');
$user->save();
// Using fill() for mass assignment
$user = User::find(1);
$user->fill([
'username' => 'updated',
'email' => '[email protected]'
])->save();
// Simple delete (no cascade)
$user = User::find(1);
$user->delete();
// Destroy with dependent cascade
$user = User::find(1);
$user->destroy(); // Cascades to related records based on dependent strategy
// Destroy multiple by IDs
User::destroyMany([1, 2, 3]); // Returns count of deleted records
User::destroyMany(1); // Can also pass single ID
// Delete via query builder
Post::where('status', 'draft')->delete(); // Returns count of deleted records
use Neuron\Orm\DependentStrategy;
DependentStrategy::Destroy // Call destroy() on each related record (cascades further)
DependentStrategy::DeleteAll // Delete with SQL (faster, no cascade)
DependentStrategy::Nullify // Set foreign key to NULL
DependentStrategy::Restrict // Prevent deletion if relations exist
use Neuron\Orm\Attributes\{Table, HasMany, HasOne, BelongsToMany};
use Neuron\Orm\DependentStrategy;
#[Table('users')]
class User extends Model
{
// Destroy: Calls destroy() on each post (cascades to post's relations)
#[HasMany(Post::class, foreignKey: 'author_id', dependent: DependentStrategy::Destroy)]
private array $_posts = [];
// DeleteAll: Fast SQL delete of profile (no cascade)
#[HasOne(Profile::class, foreignKey: 'user_id', dependent: DependentStrategy::DeleteAll)]
private ?Profile $_profile = null;
// Restrict: Prevents user deletion if comments exist
#[HasMany(Comment::class, dependent: DependentStrategy::Restrict)]
private array $_comments = [];
}
#[Table('posts')]
class Post extends Model
{
// DeleteAll: Remove pivot table entries only (genres remain)
#[BelongsToMany(Category::class, pivotTable: 'post_categories', dependent: DependentStrategy::DeleteAll)]
private array $_categories = [];
// Nullify: Set comment.post_id = NULL instead of deleting
#[HasMany(Comment::class, dependent: DependentStrategy::Nullify)]
private array $_comments = [];
}
// With Destroy strategy
$user = User::find(1);
$user->destroy(); // Deletes user, all posts, AND all post categories (nested cascade)
// With DeleteAll strategy
$post = Post::find(1);
$post->destroy(); // Deletes post AND pivot entries, but NOT the categories themselves
// With Nullify strategy
$post = Post::find(1);
$post->destroy(); // Deletes post, sets comment.post_id = NULL for all comments
// With Restrict strategy
try {
$user = User::find(1);
$user->destroy(); // Throws RelationException if user has comments
} catch (RelationException $e) {
echo "Cannot delete user: " . $e->getMessage();
}
// delete() - Simple deletion, NO cascade
$user = User::find(1);
$user->delete(); // Only deletes user, leaves posts orphaned
// destroy() - Respects dependent strategies
$user = User::find(1);
$user->destroy(); // Cascades to related records based on dependent attribute
#[Table('posts', primaryKey: 'id')]
class Post extends Model {}