PHP code example of bogdanpet / orthite-db

1. Go to this page and download the library: Download bogdanpet/orthite-db 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/ */

    

bogdanpet / orthite-db example snippets


$db = new \Orthite\Database\Database($dsn, $user, $password)

$pdo = new \PDO($dsn, $user, $password);

.  .  .

$db = new \Orthite\Database\Database($pdo);

$conn = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'port' => 3306,
    'user' => 'root',
    'password' => 'secret',
    'database' => 'database_name',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci'
];

$db = new \Orthite\Database\Database($conn);



namespace App;

class Database extends \Orthite\Database\Database
{

    protected $connection = [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'password' => 'secret',
        'database' => 'database_name'
    ];
}



namespace App;

class Database extends \Orthite\Database\Database
{

    public function __construct() {
        $this->connection['driver'] = getenv('DB_DRIVER');
        $this->connection['host'] = getenv('DB_HOST');
        $this->connection['port'] = getenv('DB_PORT');
        $this->connection['user'] = getenv('DB_USER');
        $this->connection['password'] = getenv('DB_PASSWORD');
        $this->connection['database'] = getenv('DB_NAME');
        
        parent::_construct();
    }
}

$db = new App\Database();

$data = [
    'first_name' => 'Anika',
    'age' => 28,
    'email' => '[email protected]',
    'created_at' => date()
];

$db->insert('users', $data);

$data = [
    [
        'first_name' => 'Anika',
        'age' => 28,
        'email' => '[email protected]',
        'created_at' => date()
    ],
    [
        'first_name' => 'Bob',
        'age' => 29,
        'email' => '[email protected]',
        'created_at' => date()
    ]
];

$db->insertMany('users', $data);

$users = $db->select('users');

$users = $db->select('users', ['name', 'email']);

$users = $db->limit(10)->select('users');

$users = $db->limit(10, 2)->select('users');

$users = $db->join('cities', 'city_id', 'id')->select('users');

$users = $db->table('users')->join('cities', 'city_id', 'id')->select();

$users = $db->groupBy('age')->select('users');

$users = $db->orderBy(['age', 'first_name'])->select('users');

$users = $db->orderBy(['age|ASC', 'first_name|DESC'])->select('users');

$user = $db->where('id', 3)->select('users');

$user = $db->where('id', 3)->where('age', 18, '>=', 'AND')->select('users');

$user = $db->where('id', 3)->andGreaterOrEquals('age', 18)->select('users');

$user = $db->whereId(3)->andAgeGreaterOrEquals(18)->select('users');

$users = $db->whereFirstNameLike('Ani%')->select('users');

$users = $db->whereIdIn([1, 2, 3])->select('users');

$users = $db->whereIdBetween(1, 10)->select('users');

$data = [
    'email' => '[email protected]'
];

$db->whereFirstName('Anika')->update('users', $data);

$db->whereId(8)->delete('users');

$stmt = $db->raw('SELECT * FROM users WHERE id < 100');

$users = $db->rawFetch('SELECT * FROM users WHERE id < 100');

$stmt = $db->execute('SELECT * FROM users where id = ? and first_name = ?', [$id, $first_name]);

$stmt = $db->execute('SELECT * FROM users where id = :id and first_name = :first_name', [
    ':id' => $id,
    ':first_name' => $first_name
]);

$stmt = $db->execute('SELECT * FROM users where id = ?i and first_name = ?s', [
    ':id' => $id,
    ':first_name' => $first_name
]);

$stmt = $db->execute('SELECT * FROM users where id = i:id and first_name = s:first_name', [
    ':id' => $id,
    ':first_name' => $first_name
]);

use Orthite\Database\Migrations\SchemaInterface;

$db->migrate('cities', function (SchemaInterface $schema) {
    $schema->integer('id')->unsigned()->autoIncrement()->primary();
    $schema->string('name', 50);

    return $schema;
});

$db->migrate('users', function (SchemaInterface $schema) {
    $schema->increments(); // Alias of $schema->integer('id')->unsigned()->autoIncrement()->primary();
    $schema->string('username', 40)->unique();
    $schema->string('name', 30)->nullable();
    $schema->double('score')->default('0.00');
    $schema->integer('city_id')->unsigned()->foreign('cities', 'id');

    /*
     * Adds created_at, updated_at and deleted_at columns
     * Same as:
     * $this->timestamp('created_at');
     * $this->datetime('updated_at')->nullable();
     * $this->datetime('deleted_at')->nullable();
     */
    $schema->timestamps();

    return $schema;
});