Database & Models - WaZeR-Adrien/Lightwork-api GitHub Wiki
How call database ?
Don't forget to fill the config before calling the Database class. To call the Database, you need to create or auto generate models.
How auto generate models ?
To do this, use the GeneratorFiles class (in index.php for example) like :
$generatorFiles = new GeneratorFiles(true);
$generatorFiles->run();
How to use the models ?
It's realy simple to use the models. The models extends the Database class so you can use all methods in the Database class. For example, you have a User model and you need to get all users :
$users = User::getAll();
Features of the Database :
public static getTable()
: get table which calledpublic static getColumns($table = null)
: get fields by table namepublic static where($where, $params = [], $order = null, $limit = null)
: get values with SELECT query and clause WHERE
$users = User::where('role_id > ?', [3]);
public static whereFirst($where, $params)
: get first row with SELECT query and clause WHERE like ('name = ?', ['foo'])
$user = User::whereFirst('id = ?', [3]);
public static whereLast($where, $params)
: get last row with SELECT query and clause WHERE ('name = ?', ['foo'])
$user = User::whereLast('role_id <= ?', [3]);
public static find($params, $order = null, $limit = null)
: get values by params array like (['name' => 'foo'], 'date_register DESC', 100)
$users = User::find(['role_id' => 3, 'id DESC', 50]);
public static findFirst($params)
: get first row by params array like (['id' => 1])
$user = User::findFirst(['email' => '[email protected]']);
public static findLast($params)
: get last row by params array like (['id' => 1])
$user = User::findLast(['name' => 'Foo']);
public static getById($id)
: get row by id
$user = User::getById(10);
public static getAll($order = null, $limit = null)
: get all values in table
$users = User::getAll();
public static count($where = null, $params = [])
: get number of rows with or without params
$nbUsersWithRoleLessThan4 = User::count('role_id < ?', [4]);
public store()
: insert new row and return the id inserted OR update if you give an ID to the model
$user = new User();
$user->setFirstname('Foo');
$user->setLastname('Bar');
$user->setEmail('[email protected]');
$user->store();
or
$user = new User(3); // 3 is an ID
$user->setFirstname('Foo');
$user->setLastname('Bar');
$user->setEmail('[email protected]');
$user->store();
public delete()
: delete a row
$user = new User(3);
$user->delete();
public infoFk($targetModel = [])
: get all information of foreign key with the object which contain a fk. Example : $user->infoFk()
$users = User::getAll();
foreach($users as $user) {
$user->infoFk();
or
$user->infoFk('educ_id' => 'Education'); // to call the Education model
}
public static getLastId
: get the last id insert
$userId = User::getLastId();
public static query($statement, $params = null)
: get a values with SQL statement and params
$users = User::query('SELECT * FROM user');
public static exec($statement, $params)
: execute SQL statement (insert, update...) with params
User::exec('DELETE FROM user WHERE id = ?', [2]);