Wallogit.com
2017 © Pedro Peláez
SimpleORM is a little ORM based on PHP
SimpleORM is a little object-relational mapping library written in PHP. This ORM fits me perfect but probably is too small for your projects so you can fork it and improve it or send me a pull request so I can merge changes :D, (*1)
SimpleORM is hosted in packagist so you can get it from Composer, (*2)
composer require legomolina/simple-orm
Require the Composer autoload in your index:, (*3)
require '../vendor/autoload.php';
Create your first model:, (*4)
use \SimpleORM\Model;
class MyModel extends Model
{
//Select the table which the model references
protected static function getTableName()
{
return 'my_table';
}
//OPTIONAL. Select the id field for the table. Default: 'id'
protected static function getTableId()
{
return 'my_table_id';
}
//Custom methods for this model
//public static function myMethod(my_params) { }
}
Call config method from Model to pass mysqli connection params, (*5)
\SimpleORM\Model::config(array(
'name' => 'my_database_name',
'user' => 'my_user',
'pass' => '*******',
'host' => 'my_host',
'charset' => 'charset'
));
And you are ready to use SimpleORM!, (*6)
SimpleORM has quick select methods to agilize common queries. If you want to select all from your table you don't need to type, (*7)
$result = MyModel::query()->select('*')->execute();
Just use ::all() method from \SimpleORM\Model:, (*8)
$result = MyModel::all()->execute();
Also you can find the last value of any field of your table simply calling ::getLastValue($field) from \SimpleORM\Model :, (*9)
$result = MyModel::getLastValue('my_field');
This is useful when you need the last id of your table to insert a new register when not using autoincrement., (*10)
Finally you can retrieve the register with n id with ::findId($id) from \SimpleORM\Model:, (*11)
$result = MyModel::findId(12);
If you want to select all data from your table., (*12)
$result = MyModel::query()->select('*')->execute();
If you want to add conditions. This creates a simple where (WHERE field operator value), (*13)
$result = MyModel::all()->where()->andFilter("field", "operator", "value")->execute();
If you want some conditions you can join them with AND (WHERE field operator value AND field_2 operator_2 value_2)., (*14)
$result = MyModel::all()->where()->andFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->execute();
Join them with or (WHERE field operator value AND field_2 operator_2 value_2), (*15)
$result = MyModel::all()->where()->orFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->execute();
If you want to combine both (WHERE (field operator value AND field_2 operator_2 value_2) OR (field_3 operator_3 value_3)), (*16)
$result = MyModel::all()->where()->andFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->or()->orFilter("field_3", "operator_3", "value_3")->execute();
Also you can check for NULL values (WHERE field IS NULL), (*17)
$result = MyModel::all()->where()->isNull("field")->execute();
Or you can negate (WHERE (field operator value) OR NOT (field_2 operator_2 value_2)), (*18)
$result = MyModel::all()->where()->andFilter("field", "operator", "value")->or()->not()->andFilter("field_2", "operator_2", "value_2")->execute();
If you don't want to select all fields., (*19)
$result = MyModel::query()->select('field_1', 'field_2')->where('field', '=', 'value')->execute();
If you want to order results by any field., (*20)
$result = MyModel::all()->order('field', 'ASC')->execute();
$result = MyModel::all()->order(['field_1', 'field_2'], ['ASC', 'DESC'])->execute();
If you want to limit the results returned., (*21)
$result = MyModel::all()->get(1)->execute(); //get 1 without offset $result = MyModel::all()->get(1, 2)->execute(); //get 1 with offset 2
If you want to insert values., (*22)
$insert = array('field' => 'value', 'field' => 'value');
$result = MyModel::query()->insert($insert)->execute();
//$result => true if insertion is correct, false otherwise
If you want to delete items., (*23)
$result = MyModel::query()->delete()->where('field', '=', 'value')->execute(); //important use where() with delete()
//$result => true if delete is correct, false otherwise
If you want to update items., (*24)
$update = array('field' => 'value', 'field' => 'value');
$result = MyModel::query()->update($update)->where('field', '=', 'value')->execute(); //important use where() with update()
//$result => true if update is correct, false otherwise
ResultSet is a handler class for Select queries. It allows you to loop through results, find value or checks if exists some field., (*25)
Easiest way doing this is with ResultSet->loop() method inside while loop., (*26)
while($result->loop()) {
$field_1 = $result->table_field_1;
$field_2 = $result->table_field_2;
...
$field_n = $result->table_field_n;
//do something with the values
}
ResultSet->loop() loops through all registers in the ResultSet and each iteration it loads next register values., (*27)
You can also go to n register executing, (*28)
$result->goToRegister(n); $result->table_field_1; $result->table_field_2;
Or you can loop manually with, (*29)
$result->first(); //loads first register $result->next(); //loads next register if exists, otherwise it will return false $result->prev(); //loads previous register if exists, otherwise it will return false $result->last(); //loads last register
You can check manually if the current register is the first or the last., (*30)
$result->isFirst(); //true | false $result->isLast(); //true | false
With ResultSet you can search for a specific value in all results from database and return the register it belongs to., (*31)
$result->find('table_field', 'find_this_value'); //returns false if doesn't find anything
Also you can know if a field exists., (*32)
$result->fieldExists('table_field'); //true if exists, false otherwise
And finally you can search a value from ALL registers. It will return the first register that founds with this value, (*33)
$result->findValue('find_this_value');
SimpleORM is licensed under the MIT license. See License File for more information., (*34)