2017 © Pedro Peláez
 

library simple-orm

SimpleORM is a little ORM based on PHP

image

legomolina/simple-orm

SimpleORM is a little ORM based on PHP

  • Sunday, May 13, 2018
  • by legomolina
  • Repository
  • 1 Watchers
  • 1 Stars
  • 45 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 24 Versions
  • 0 % Grown

The README.md

SimpleORM

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)

Installation

SimpleORM is hosted in packagist so you can get it from Composer, (*2)

composer require legomolina/simple-orm

Configure

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)

Usage

Quick access methods

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);

Select queries (See quick access)

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

Data manipulation

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

Working with ResultSet

ResultSet is a handler class for Select queries. It allows you to loop through results, find value or checks if exists some field., (*25)

Getting values from result

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

Search for a value

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');

License

SimpleORM is licensed under the MIT license. See License File for more information., (*34)

The Versions

13/05 2018

dev-master

9999999-dev

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

22/03 2017

2.0.1

2.0.1.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

22/03 2017

dev-dev

dev-dev

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

17/02 2017

2.0.0

2.0.0.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

11/02 2017

1.2.6

1.2.6.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

09/02 2017

1.2.5

1.2.5.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

01/11 2016

1.2.4

1.2.4.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

30/10 2016

1.2.3

1.2.3.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

28/10 2016

1.2.2

1.2.2.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

27/10 2016

1.2.1

1.2.1.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

27/10 2016

1.2.0

1.2.0.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

27/10 2016

1.1.5

1.1.5.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

27/10 2016

1.1.4

1.1.4.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

27/10 2016

1.1.3

1.1.3.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

26/10 2016

1.1.2

1.1.2.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

26/10 2016

1.1.1

1.1.1.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.1.0

1.1.0.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.7

1.0.7.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.6

1.0.6.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.5

1.0.5.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.4

1.0.4.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.3

1.0.3.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

25/10 2016

1.0.2

1.0.2.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple

24/10 2016

1

1.0.0.0

SimpleORM is a little ORM based on PHP

  Sources   Download

MIT

The Requires

  • php >=7.0

 

by Cristian Molina

orm php simple