, (*1)
Simple ORM
Simple ORM is an object-relational mapper for PHP & MySQL (using mysqli). It
provides a simple way to create, retrieve, update & delete records., (*2)
This is not intended for large scale projects as it has extra database interaction than necessary. I would suggest Doctrine for such things., (*3)
Simple ORM is a few years old but it has been upgraded over time, therefore PHP 5.3 is a requirement., (*4)
Configuration
To utilise this tool you need to do a few things:, (*5)
- Include the class file.
- Create a
mysqli object.
- Tell
SimpleOrm to use the mysqli connection.
For example:, (*6)
// Include the Simple ORM class
include 'SimpleOrm.class.php';
// Connect to the database using mysqli
$conn = new mysqli('host', 'user', 'password');
if ($conn->connect_error)
die(sprintf('Unable to connect to the database. %s', $conn->connect_error));
// Tell Simple ORM to use the connection you just created.
SimpleOrm::useConnection($conn, 'database');
Object/Table Definition
Define an object that relates to a table., (*7)
class Blog extends SimpleOrm { }
Basic Usage
Create an entry:, (*8)
$entry = new Blog;
$entry->title = 'Hello';
$entry->body = 'World!';
$entry->save();
Retrieve a record by it's primary key:, (*9)
$entry = Blog::retrieveByPK(1);
Retrieve a record using a column name:, (*10)
$entry = Blog::retrieveByTitle('Hello', SimpleOrm::FETCH_ONE);
Update a record:, (*11)
$entry->body = 'Mars!';
$entry->save();
Delete the record:, (*12)
$entry->delete();
Class Configuration
This section will detail how you define your objects and how they relate to your MySQL tables., (*13)
A Basic Object
class Foo extends SimpleOrm {}
Class Naming
The following assumptions will be made for all objects:, (*14)
- The database used is the database loaded in the
mysqli object.
- The table name is the class name in lower case.
- The primary key is
id.
Customisation
You can customise the assumptions listed above using the following static properties:, (*15)
For example:, (*16)
class Foo extends SimpleOrm
{
protected static
$database = 'test',
$table = 'foobar',
$pk = 'fooid';
}
Data Manipulation
This section will detail how you modify your records/objects., (*17)
Creating/Inserting New Records
You can start a new instance & save the object or you can feed it an array., (*18)
$foo = new Foo;
$foo->title = 'hi!';
$foo->save();
or, (*19)
$foo = new Foo(array('title'=>'hi!'));
$foo->save();
Updating
Simply modify any property on the object & use the save() method., (*20)
$foo->title = 'hi!';
$foo->save();
If you want to have some more control over manipulating data you can use set(), get() & isModified()., (*21)
$foo->set('title', 'hi!');
$foo->save();
Deleting
Use the delete() method., (*22)
$foo->delete();
Data Retrieval
This section will detail how you fetch data from mysql and boot your objects., (*23)
Using the Primary Key
$foo = Foo::retrieveByPK(1);
or, (*24)
$foo = new Foo(1);
Using a Column Name
$foo = Foo::retrieveByField('bar', SimpleOrm::FETCH_ONE);
By default, the retrieveBy* method will return an array of objects (SimpleOrm::FETCH_MANY)., (*25)
Select All
$foo = Foo::all();
Fetch Constants
SimpleOrm::FETCH_ONE will return a single object or null if the record is not found., (*26)
SimpleOrm::FETCH_MANY will always return an array of hydrated objects., (*27)
Populating from an Array (Hydration)
You can pass in an associative array to populate an object. This saves retrieving a record each time from the database., (*28)
$foo = Foo::hydrate($array);
SQL Statements
Any SQL statement can be used as long as all the returning data is for the object., (*29)
Example, (*30)
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'");
This will return an array of hydrated Foo objects., (*31)
If you only want a single entry to be returned you can request this., (*32)
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'", SimpleOrm::FETCH_ONE);
SQL Tokens
The table name & primary key have shortcuts to save you writing the same names over & over in your SQL statements:, (*33)
-
:database will be replaced with the database name.
-
:table will be replaced with the table name.
-
:pk will be replaced with the primary key field name.
Any extra data that does not belong to object being loaded will have those fields populated in the object:, (*34)
$foo = Foo::sql("SELECT *, 'hi' AS otherData FROM :table WHERE foo = 'bar'", SimpleOrm::FETCH_ONE);
echo $foo->otherData; // returns 'hi'
This can be useful if you plan to use aggregate functions within your object & you want to pre-load the data., (*35)
Filters
Input & output filters can be created to alter data going into the database & when it comes out., (*36)
To add a filter, you only need to create a method with either filterIn or filterOut as a prefix (e.g. filterIn, filterInHi, filterIn_hi)., (*37)
These methods will automatically fire when data is loaded into the object (hydration) or saved into the database (save, update, insert)., (*38)
Data being saved to the database can be modified., (*39)
For example:, (*40)
class Foo extends SimpleOrm
{
protected function filterIn_dates ($data)
{
$data['updated_at'] = time();
return $data;
}
}
In the example above, every time the object is saved, the updated_at field is populated with a current time & date., (*41)
Note: You must return the input array otherwise no fields will be updated., (*42)
Output Filters
Any data loaded into the object will be passed through any output filters., (*43)
class Foo extends SimpleOrm
{
protected function filterOut ()
{
$this->foo = unserialize($this->foo);
}
}
In the example above, each time the object is hydrated, the foo property is unserialized., (*44)