PHP code example of legomolina / simple-orm

1. Go to this page and download the library: Download legomolina/simple-orm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

legomolina / simple-orm example snippets



    
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) { }
}

\SimpleORM\Model::config(array(
    'name' => 'my_database_name',
    'user' => 'my_user',
    'pass' => '*******',
    'host' => 'my_host',
    'charset' => 'charset'
));

$result = MyModel::query()->select('*')->execute();

$result = MyModel::all()->execute();

$result = MyModel::getLastValue('my_field');

$result = MyModel::findId(12);

$result = MyModel::query()->select('*')->execute();

$result = MyModel::all()->where()->andFilter("field", "operator", "value")->execute();

$result = MyModel::all()->where()->andFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->execute();

$result = MyModel::all()->where()->orFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->execute();

$result = MyModel::all()->where()->andFilter([["field", "operator", "value"], ["field_2", "operator_2", "value_2"]])->or()->orFilter("field_3", "operator_3", "value_3")->execute();

$result = MyModel::all()->where()->isNull("field")->execute();

$result = MyModel::all()->where()->andFilter("field", "operator", "value")->or()->not()->andFilter("field_2", "operator_2", "value_2")->execute();

$result = MyModel::query()->select('field_1', 'field_2')->where('field', '=', 'value')->execute();

$result = MyModel::all()->order('field', 'ASC')->execute();
$result = MyModel::all()->order(['field_1', 'field_2'], ['ASC', 'DESC'])->execute();

$result = MyModel::all()->get(1)->execute(); //get 1 without offset
$result = MyModel::all()->get(1, 2)->execute(); //get 1 with offset 2

$insert = array('field' => 'value', 'field' => 'value');
$result = MyModel::query()->insert($insert)->execute();

//$result => true if insertion is correct, false otherwise

$result = MyModel::query()->delete()->where('field', '=', 'value')->execute(); //important use where() with delete()

//$result => true if delete is correct, false otherwise

$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

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
}

$result->goToRegister(n);

$result->table_field_1;
$result->table_field_2;

$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

$result->isFirst(); //true | false
$result->isLast();  //true | false

$result->find('table_field', 'find_this_value'); //returns false if doesn't find anything

$result->fieldExists('table_field'); //true if exists, false otherwise

$result->findValue('find_this_value');
::getLastValue($field)
::findId($id)