Config Database ConnectionLast updated: 04/10/2020

Config File Content

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_OBJ, // for array -> PDO::FETCH_ASSOC,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish.
    */

    'default' => 'mysql',


    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by PHPtricks/database class is shown below to make development simple.
    |
    |
    | All database work in HPtricks/database is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [
        // MySQL >= 3.0.0
        'mysql'  => [
            'driver'      => 'mysql',
            'host_name'   => 'localhost',
            'db_name'     => 'database_name',
            'db_user'     => 'database_user',
            'db_password' => 'database_user_password',
        ],

        // PostgreSQL
        'pgsql'  => [
            'driver'      => 'pgsql',
            'host_name'   => 'localhost',
            'db_name'     => 'database_name',
            'db_user'     => 'database_username',
            'db_password' => 'database_user_password',
        ],

        // SQLite
        'sqlite' => [
            'db_path' => 'my/database/path/database.db',
        ],

        //	MS SQL Server
        'mssql'  => [
            'driver'      => 'mssql',
            'host_name'   => 'localhost',
            'db_name'     => 'database_name',
            'db_user'     => 'database_username',
            'db_password' => 'database_user_password',
        ],

        //	MS SQL Server
        'sybase' => [
            'driver'      => 'sybase',
            'host_name'   => 'localhost',
            'db_name'     => 'database_name',
            'db_user'     => 'database_username',
            'db_password' => 'database_user_password',
        ],

        // Oracle Call Interface
        'oci'    => [
            'tns' => '
					DESCRIPTION =
					    (ADDRESS_LIST =
					      (ADDRESS = (PROTOCOL = TCP)(HOST = yourip)(PORT = 1521))
					    )
					    (CONNECT_DATA =
					      (SERVICE_NAME = orcl)
					    )
					  )',

            'db_user'     => 'database_username',
            'db_password' => 'database_user_password',
        ],
    ],


    "pagination" => [
        "no_data_found_message" => "Oops, No Data Found to show ..",
        "records_per_page"      => 10,
        "link_query_key"        => "page",
    ],

    "search"      => [
        "key"    => "search",
        "method" => "get",
    ],

    // Directories for Commands
    'directories' => [
        'create' => __DIR__.'/../Migrations/create/',
        'alter'  => __DIR__.'/../Migrations/alter/',
        'drop'   => __DIR__.'/../Migrations/drop/',
        'migrated-file' =>  __DIR__.'/../Migrations/temp/'
    ],
];

set your configuration, to connect with database.

Include Library

Include Library Files in your Project.

if you using composer to download library simply include vendor/autoload.php file
if you Downloaded library without composer, you want to include vendor/autoload.php file from the library directory.

you can use require_once function to include library files.
// using composer 
include_once ('vendor/autoload.php');

// without composer
include_once ('PHPtricksORM/vendor/autoload.php');

Create Your First Table

to create table, you want to create migration first.

to create migration, use command-line

# create migration to create table command
php phptricks migrate:make  CreateUsersTable create

this command will generate class file file directory/name: Migrations/create/CreateUsersTable.php class name: CreateUsersTable


you can send --table=table_name or -t=table_name or -t table_name
set your table name

# set table name fo migration command
php phptricks migrate:make  CreateUsersTable create --t=users


Generated Class :

<?php

require_once 'vendor/autoload.php';

use PHPtricks\Orm\Builder;
class CreateUsersTable extends Builder
{
    // if -table option sent
    protected $_table = 'users';
    // if -table option not sent
    // protected $_table = 'table_name';

    public function run()
    {
        return $this->schema([
            'id' => 'increments',
            'string' => 'string:255|not_null|unique',
            'datetime' => 'datetime',
            'integer' => 'int|unsigned',
            'unique' => 'string:255|not_null|unique',
            'created_at' => 'timestamp',
        ])->create();
    }
}

change table schema to meet what you want

run migrate command to generate table in database.

php phptricks migrate

Create Your First Model

you can create model manually, or using command-line

# generate model class
php phptricks model UserModel

you can send table name via command

# generate model class with table name
php phptricks model UserModel --table=users


Generated Class

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

and now you can use this model to control users table

Insert Data in Users Table

to insert data we want to use created model.

#inside model
use PHPtricks\Orm\Model;
class UserModel extends Model
{
    protected $_table = 'users';
    
    public function generateFiveUsersForTesting()
    {
        for( $i = 0; $i < 6; $i++ ) {
          $this->insert([
            'username' => "user number $i",
            'email' => "email-{$i}@phptricksORM.test",
          ]);
        }
    }
}

and now, call the method

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

you can insert users outside model

$users = new UserModel();
$users->insert([
  'username' => 'Mohammad Anzawi',
  'email' => 'm.anzawi2013@gmail.com',
]);



Update User

$users = new UserModel();
$users->where('id', 1) // you can use ( find(), findBy() )
       ->update([
  'username' => 'Mohammad AL-Anzawi',
]);

#======== OR ==========#
$users = new UserModel();
$user = $users->find(1); // you can use ( where(), findBy() )
$user->username = 'Mohammad AL-Anzawi';
$user->save();



Delete User

$users = new UserModel();
$users->where('id', 1) // you can use ( find(), findBy() )
       ->delete();

#======== OR ==========#
$users = new UserModel();
$user = $users->find(1); // you can use ( where(), findBy() )
$user->delete();