dev-master
9999999-devA simple PHP class to access your MySQL records.
MIT
by Alex Joyce
orm database mysql
A simple PHP class to access your MySQL records.
Simple ORM is an object-relational mapper for PHP & MySQL (using mysqli). It provides a simple way to create, retrieve, update & delete records., (*1)
This is not intended for large scale projects as it has extra database interaction than necessary. I would suggest Doctrine for such things., (*2)
Simple ORM is a few years old but it has been upgraded over time, therefore PHP 5.3 is a requirement., (*3)
To utilise this tool you need to do a few things:, (*4)
mysqli
object.SimpleOrm
to use the mysqli
connection.For example:, (*5)
// 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');
Define an object that relates to a table., (*6)
class Blog extends SimpleOrm { }
Create an entry:, (*7)
$entry = new Blog; $entry->title = 'Hello'; $entry->body = 'World!'; $entry->save();
Retrieve a record by it's primary key:, (*8)
$entry = Blog::retrieveByPK(1);
Retrieve a record using a column name:, (*9)
$entry = Blog::retrieveByTitle('Hello', SimpleOrm::FETCH_ONE);
Update a record:, (*10)
$entry->body = 'Mars!'; $entry->save();
Delete the record:, (*11)
$entry->delete();
This section will detail how you define your objects and how they relate to your MySQL tables., (*12)
class Foo extends SimpleOrm {}
The following assumptions will be made for all objects:, (*13)
mysqli
object.id
.You can customise the assumptions listed above using the following static properties:, (*14)
For example:, (*15)
class Foo extends SimpleOrm { protected static $database = 'test', $table = 'foobar', $pk = 'fooid'; }
This section will detail how you modify your records/objects., (*16)
You can start a new instance & save the object or you can feed it an array., (*17)
$foo = new Foo; $foo->title = 'hi!'; $foo->save();
or, (*18)
$foo = new Foo(array('title'=>'hi!')); $foo->save();
Simply modify any property on the object & use the save() method., (*19)
$foo->title = 'hi!'; $foo->save();
If you want to have some more control over manipulating data you can use set(), get() & isModified()., (*20)
$foo->set('title', 'hi!'); $foo->save();
Use the delete() method., (*21)
$foo->delete();
This section will detail how you fetch data from mysql and boot your objects., (*22)
$foo = Foo::retrieveByPK(1);
or, (*23)
$foo = new Foo(1);
$foo = Foo::retrieveByField('bar', SimpleOrm::FETCH_ONE);
By default, the retrieveBy* method will return an array of objects (SimpleOrm::FETCH_MANY)., (*24)
$foo = Foo::all();
SimpleOrm::FETCH_ONE
will return a single object or null if the record is not found., (*25)
SimpleOrm::FETCH_MANY
will always return an array of hydrated objects., (*26)
You can pass in an associative array to populate an object. This saves retrieving a record each time from the database., (*27)
$foo = Foo::hydrate($array);
Any SQL statement can be used as long as all the returning data is for the object., (*28)
Example, (*29)
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'");
This will return an array of hydrated Foo objects., (*30)
If you only want a single entry to be returned you can request this., (*31)
$foo = Foo::sql("SELECT * FROM :table WHERE foo = 'bar'", SimpleOrm::FETCH_ONE);
The table name & primary key have shortcuts to save you writing the same names over & over in your SQL statements:, (*32)
: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:, (*33)
$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., (*34)
Input & output filters can be created to alter data going into the database & when it comes out., (*35)
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)., (*36)
These methods will automatically fire when data is loaded into the object (hydration) or saved into the database (save, update, insert)., (*37)
Data being saved to the database can be modified., (*38)
For example:, (*39)
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., (*40)
Note: You must return the input array otherwise no fields will be updated., (*41)
Any data loaded into the object will be passed through any output filters., (*42)
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., (*43)
A simple PHP class to access your MySQL records.
MIT
orm database mysql