Introduction

Objectiphy is a simple data mapper ORM - inspired by (and somewhat compatible with) the Doctrine project, with the aim of being easy to learn, intuitive, and flexible.

Create repositories using the repository factory:

use Objectiphy\Objectiphy\Factory\RepositoryFactory;
use MyProject\Contact;

$pdo = new \PDO(
    'mysql:host=localhost;dbname=test', 
    getenv('DB_USER'), 
    getenv('DB_PASS')
);

$factory = new RepositoryFactory($pdo);
$repository = $factory->createRepository(Contact::class);

You can use simple criteria in an array to load entities:

// Load all staff in Sales department:
$criteria = ['department.name' => 'Sales'];
$contacts = $repository->findBy($criteria);

Or use more complex criteria to limit which properties of which records are returned using the object-oriented query builder, which closely resembles standard SQL syntax (but using classes and properties instead of tables and columns):

// Get just the name for permanent staff in Sales and 
// Finance who have a car (even if the Contact entity 
// does not have a vehicle property)
$criteria = ['departments' => ['Sales', 'Finance']];
$query = QB::create()
         ->select('firstName', 'lastName')
         ->from(Contact::class)
         ->innerJoin(Vehicle::class, 'v')
             ->on('id', '=', 'v.ownerContactId')
             ->and('v.type', '=', 'car')
         ->where('department.name', 'IN', ':departments')
         ->and('isPermanent', '=', true)
         ->buildSelectQuery($criteria);
 $contacts = $repository->findBy($query);

Inserting or updating entities is also very easy (Objectiphy keeps track of changes to entities that it loaded, so will only save the things that have changed):

$repository->saveEntity($contact);

Mapping information is typically specified using mapping definitions. You can use entities with Objectiphy or Doctrine attributes or annotations (some features of Objectiphy that are not present in Doctrine require Objectiphy attributes, but you can mix both types).

Features

This library has the ability to insert, update, and delete records in your database. Use with care!

Last updated