π Getting Started
Introduction
Hector ORM is a lightweight, framework-agnostic PHP ORM β designed to be modular, fast, and expressive. It draws inspiration from existing ORM concepts, while promoting freedom of structure and strong typing.
Requirements
- PHP 8.0+
- PDO extension
- Database driver (e.g.,
pdo_mysql,pdo_sqlite)
What is an ORM?
Object-relational mapping (ORM, O/RM, and O/R mapping tool!) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a βvirtual object databaseβ that can be used from within the programming language.
β Wikipedia
β¨ Choose Your Style
You can manage entities in multiple ways:
- Classic entities: define PHP properties explicitly and Hector ORM handles mapping.
- Magic entities: rely on Hector ORM dynamic behavior using PHPβs magic methods.
- Roll your own π§ͺ: create a custom Mapper if you want total control over mapping logic.
Why Hector ORM?
| Feature | Hector ORM | Doctrine | Eloquent |
|---|---|---|---|
| Zero config | β | β | β οΈ |
| Framework-agnostic | β | β | β |
| Magic + Classic entities | β | β | β |
| Schema introspection | β | β | β |
Quick Start
1. π¦ Installation
Install with Composer:
composer require hectororm/hectororm
2. Create a Database Connection
use Hector\Connection\Connection;
$connection = new Connection('mysql:host=localhost;dbname=test', 'user', 'pass');
3. Boot the ORM
use Hector\Orm\OrmFactory;
$orm = OrmFactory::orm([
'schemas' => ['my-schema'] // Database name(s) to introspect
], $connection);
4. π§± Define Entities
use Hector\Orm\Attributes as Orm;
use Hector\Orm\Entity\MagicEntity;
#[Orm\HasOne(Bar::class, 'bar')]
class Foo extends MagicEntity {}
#[Orm\BelongsTo(Foo::class, 'foo')]
class Bar extends MagicEntity {}
5. π‘ Use the ORM
$foo = Foo::findOrFail(1); // find a Foo entity by primary key
$bar = $foo->bar; // access related Bar entity
echo $bar->field; // access a field from the related Bar
Youβre now ready to build with Hector ORM. Keep exploring:
Happy mapping πΊοΈ