Introduction Last updated: 04/10/2020

PHPtricks-ORM make transaction with database simple and easy with efficient,
Each database table has a corresponding "Model" which is used to interact with that table.
Models allow you to query for data in your tables, as well as (insert, update, delete) records.

Create Models

Before getting started, be sure to configure a database connection and models path in Config/database_config.php.
For more information on configuring your database, check out the documentation.
to create model using command
php phptricks model UserModel --table=users


use PHPtricks\Orm\Model;
class UserModel extends Model
{
    protected $_table = 'users';
}

use optional flag -table=table_name or --t=table_name or --t table_name to define property protected $_table


without defining protected $_table he model cannot specify which table to perform operations on

Primary Key :

PHPtricks will also assume that each table has a primary key column named id.
You may define a protected $_idColumn property to override this convention.

Retrieving Models

for any explain for examples under this section pleas check out DML operations.

Get Data :


$users = new UserModel();
$users->select();

foreach ( $users as $user ) {
    echo $user->username;
}


Adding Additional Constraints
$users = new UserModel();
$users->where('active', 0)
    ->orWhere('email', null)
    ->limit(10)
    ->offset(5)
    ->orderBy('username', 'ASC')
    ->select();


Get First Record
$users = new UserModel();
$users->where('username', 'mohammad')
    ->first();
   // ->select()->first(); # this will work too


Paginate Retrieved Data :
$users = new UserModel();
$paginateUsers = $users->paginate();

pleas note: (select, first, find, findBy, paginate) methods return instance of Collection Class
Learn More About Collection


Get Specified Columns :
$users = new UserModel();

$allUsers      = $users->select(['username', 'email']);
$firstUser     = $users->first(['username', 'email']);
$paginateUsers = $users->paginate(['username', 'email']);
$findUser      = $users->find(['username', 'email']);

Check Out full documentation of Retrieving Data



Collections :

For PHPtricks-ORM methods like (all, get, map and filter, ...)
which retrieve multiple results, an instance of PHPtricksOrm\Collection\Collection will be returned.
The Collection class provides a variety of helpful methods for working with your results:

$users = new UserModel();

$results = $users->select();

$filteredResults = $results->filter(function($row, $key) {
    // you can use $key if you want 
    return $row->id > 15;
});

var_dump($filteredResults);


Collections Documentation

Insert , Update & Delete Models


Insert :
$userModel = new UserModel();

$userModel->insert([
    'username' => 'Mohammad',
    'email'    => 'email@mohammad.test',
    'active'   => 1
]);


Update :
$userModel = new UserModel();

$user = $userModel->find(1);

$user->update([
    'username' => 'Mohammad-phptricks',
]);


Direct Update :
$userModel = new UserModel();

$user = $userModel->find(1);

$user->username = 'Anzawi';
$user->save();
$userModel = new UserModel();

$user = $userModel->where('username', 'mohammad')->first();

$user->username = 'Anzawi';
$user->save();



Delete :
$userModel = new UserModel();

$userModel->find(1)->delete();
$userModel = new UserModel();

$user = $userModel->where('username', 'Anzawi')->first();
$user->delete();