dev-master
9999999-devA compact PHP Object Relational Management library
BSD-3-Clause
The Requires
- php >=5.3.0
- aura/sql ~2.0
- aura/sqlquery ~2.0
The Development Requires
orm database
A compact PHP Object Relational Management library
Last Updated: 2014-05-02, Chris Tankersley, (*1)
Casket is a compact Object Relational Management library for PHP. It allows for quick prototyping and management of objects that need to be persisted. Casket is a fork of PhpORM, an older ORM I created. You can find it at https://github.com/dragonmantank/PhpORM, (*4)
Since persistence doesn't always mean to a database, it can easily be extended to support different persistance layers., (*5)
This version uses Aura.SQL to provide support for any database provider supported by Aura.SQL. All you need is PDO!, (*6)
Entities are the 'things' in the application. If you are building a pet website, Entities would be something like a 'Animal', 'AdoptionAgency', 'Address'. Entities are a single object that needs to be persisted, more normally in a database., (*7)
An Entity would look like this:, (*8)
Class Animal { public $id; // Database ID public $type; // Type of animal public $inductionDate // Date Animal came to Shelter public $name; // Name of the animal public $shelter_id // Foreign key of the animal shelter }
You could then access the entity like this:, (*9)
$animal = new Animal(); $animal->type = 'Cat'; $animal->inductionDate = time(); $animalRepository->save($animal);
Repositories are the recommended way to get information out of the data storage. We've done away with the need for using direct Data Access Objects. Repositories are generally set around a specific Entity and work only with that Entity. This allows the Repository to automatically generate Entities based on the data sources., (*10)
For database-backed persistance, a basic repository needs no specific class and can utilize the shipped DBRepository class. It requires just a storage mechanism and a prototype object to base it's results on., (*11)
// $storage is a pre-created connection to your data store, see the Storage section $animalRepo = new Casket\Repository\DBRepository($storage, new Animal());
This repository will return Animal entities (or collections of Animal entities):, (*12)
$cats = $animalRepo->fetchAllBy(array('type' => 'cat')); // Return all the cats $dog = $repo->find($dogId); // Find the specified object by primary key $fluffy = $repo->findBy(array('name' => 'Fluffy')); // Search by something other than primary key
Storage systems provide a standard way to interact with some sort of storage system, be it a database or an API. Casket ships with a PDO-based storage system that utilizes Aura.SQL and Aura.SQL-Query to provide access to many different database backends., (*13)
For the AuraExtendedPdo storage system, you just need to invoke it with an AuraExtendedPdo object and a QueryFactory from Aura.SQL-Query., (*14)
$storage = new Casket\Storage\AuraExtendedPdo($extendedPdo, new Aura\SqlQuery\QueryFactory('mysql'));
You can then use the storage with your Repositories so they can access data:, (*15)
$animalRepo = new Casket\Repository\DBRepository($storage, new Animal());
A compact PHP Object Relational Management library
BSD-3-Clause
orm database