Skip to content
Bastien Crettenand edited this page Aug 28, 2019 · 10 revisions

Prerequisites

  • An already structured database must exist
  • A working configuration file

How to use? 🤔

Create new Model

Example 1 :

use slsql\Model\Model;

class Users extends Model
{
    public $name, $psw, $email, $id;
}

Example 2 :

use slsql\Model\Model;

class Users extends Model
{
    public $name, $firstName, $psw, $email, $id;

    public function getInitials(){
       $initials = substr($this->firstName, 0, 3);
       return $initials .= substr($this->name, 0, 3);
    }
}

Use new Model

Example 1 :

$user = new Users();
$user->name = "CreBast";
$user->psw = "123";
$user->email = "[email protected]";
$user->save();

Example 2 :

$user = Users::get()->firstOrDefault()

Methods

Model

save()

Save object on database :

$user = new Users();
$user->name = "Bastien"; // Actually not on DB
$user->save(); // Now yes

get(condition = null, $arr = array())

Get list of 'Users' with condition :

$user = Users::get('id = ? or id = ?', array(1, 2));
// Return ListModels() with users with ids 1 and 2 (if exist)

remove()

Remove object on database :

$user = Users::get('id = ?', array(1))->first();
$user->remove()
// Remove user with id 1

ids()

Return all ids on one list :

$user_ids = Users::ids();
// Return : array(1, 2, 3, ...)

all(field = 'id')

Return all values of field on list :

$all_names = Users::all("name");
// Return : array(Bastien, José, Bastien, ...)

allDistinct()

Return all values of field on list (distinct) :

$all_names = Users::allDistinct("name");
// Return : array(Bastien, José, ...)

count()

Count number of objects :

$number = Users::count();
// Return : number

countWhere(condition = null, $arr = array())

Count with condition :

$number = Users::countWhere('name = ?', array('Bastien'));
// Return : number

ListModels

add(Model)

Add new elements on ListModels() :

$listModels = new ListModels;
$listModels->add(new Users);

all()

Return one list with all datas :

$users = Users::get()->all();
// Return all users

firstOrDefault(default value = null)

Return the first user returned. With default value if nothing :

$user = Users::get()->firstOrDefault(false);
// Return the first user returned. If nothing : false

first()

Return the first user returned :

$user = Users::get()->first();
// Return the first user returned. If nothing : null

lastOrDefault(default value = null)

Return the last user returned. With default value if nothing :

$user = Users::get()->lastOrDefault(false);
// Return the last user returned. If nothing : false

last()

Return the last user returned :

$user = Users::get()->last();
// Return the last user returned. If nothing : null