PHP code example of penobit / idb

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

    

penobit / idb example snippets


// Make sure you have Composer's autoload file included
     'driver'    => 'mysql', // Db driver
            'host'      => 'localhost',
            'database'  => 'your-database',
            'username'  => 'root',
            'password'  => 'your-password',
            'charset'   => 'utf8', // Optional
            'collation' => 'utf8_unicode_ci', // Optional
            'prefix'    => 'cb_', // Table prefix, optional
            'options'   => [ // PDO constructor options, optional
                PDO::ATTR_TIMEOUT => 5,
                PDO::ATTR_EMULATE_PREPARES => false,
            ],
        ];

new \iDB\Connection('mysql', $config, 'QB');

$row = QB::table('my_table')->find(3);

$query = QB::table('my_table')->where('name', '=', 'Sana');

// Get result
$query->get();

QB::registerEvent('before-select', 'users', function($qb)
{
    $qb->where('status', '!=', 'banned');
});


// Make sure you have Composer's autoload file included
 Db driver
            'host'      => 'localhost',
            'database'  => 'your-database',
            'username'  => 'root',
            'password'  => 'your-password',
            'charset'   => 'utf8', // Optional
            'collation' => 'utf8_unicode_ci', // Optional
            'prefix'    => 'cb_', // Table prefix, optional
        );

new \iDB\Connection('mysql', $config, 'QB');

// Run query
$query = QB::table('my_table')->where('name', '=', 'Sana');

new \iDB\Connection('mysql', $config, 'MyAlias');

$connection = new \iDB\Connection('mysql', $config);
$qb = new \iDB\QueryBuilder\QueryBuilderHandler($connection);

$query = $qb->table('my_table')->where('name', '=', 'Sana');

var_dump($query->get());

new \iDB\Connection('sqlite', array(
                'driver'   => 'sqlite',
			    'database' => 'your-file.sqlite',
			    'prefix'   => 'cb_',
		    ), 'QB');

new \iDB\Connection('pgsql', array(
                    'driver'   => 'pgsql',
                    'host'     => 'localhost',
                    'database' => 'your-database',
                    'username' => 'postgres',
                    'password' => 'your-password',
                    'charset'  => 'utf8',
                    'prefix'   => 'cb_',
                    'schema'   => 'public',
                ), 'QB');

QB::table(array('mytable1', 'mytable2'));

$row = QB::table('my_table')->find(3);

$result = QB::table('my_table')->findAll('name', 'Sana');

$query = QB::table('my_table')->select('*');

->select(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));

->selectDistinct(array('mytable.myfield1', 'mytable.myfield2'));

$query = QB::table('my_table')->where('name', '=', 'Sana');
$result = $query->get();

foreach ($result as $row) {
    echo $row->name;
}

$query = QB::table('my_table')->where('name', '=', 'Sana');
$row = $query->first();

$query = QB::table('my_table')->where('name', '=', 'Sana');
$query->count();

QB::table('my_table')
    ->where('name', '=', 'penobit')
    ->whereNot('age', '>', 25)
    ->orWhere('type', '=', 'admin')
    ->orWhereNot('description', 'LIKE', '%query%')
    ;

QB::table('my_table')
    ->whereIn('name', array('penobit', 'sana'))
    ->orWhereIn('name', array('heera', 'dalim'))
    ;

QB::table('my_table')
    ->whereNotIn('name', array('heera', 'dalim'))
    ->orWhereNotIn('name', array('penobit', 'sana'))
    ;

QB::table('my_table')
    ->where('json_data->username', 'R8')
;

QB::table('my_table')
    ->whereNotIn('json_data->username', ['Arash'])
;

QB::table('my_table')
    ->whereBetween('id', 10, 100)
    ->orWhereBetween('status', 5, 8);

QB::table('my_table')
    ->whereNull('modified')
    ->orWhereNull('field2')
    ->whereNotNull('field3')
    ->orWhereNotNull('field4');

QB::table('my_table')
            ->where('my_table.age', 10)
            ->where(function($q)
                {
                    $q->where('name', 'LIKE', '%penobit%');
                    // You can provide a closure on these wheres too, to nest further.
                    $q->orWhere('description', 'LIKE', '%penobit%');
                });

$query = QB::table('my_table')->groupBy('age')->orderBy('created_at', 'ASC');

->groupBy(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));

->orderBy(array('mytable.myfield1', 'mytable.myfield2', 'another_table.myfield3'));

->having('total_count', '>', 2)
->orHaving('type', '=', 'admin');

->limit(30);

->offset(10);

QB::table('my_table')
    ->join('another_table', 'another_table.person_id', '=', 'my_table.id')


->join('another_table', 'another_table.person_id', '=', 'my_table.id', 'FULL OUTER')

->join('another_table', function($table)
    {
        $table->on('another_table.person_id', '=', 'my_table.id');
        $table->on('another_table.person_id2', '=', 'my_table.id2');
        $table->orOn('another_table.age', '>', QB::raw(1));
    })

$query = QB::query('select * from cb_my_table where age = 12');

var_dump($query->get());

QB::query('select * from cb_my_table where age = ? and name = ?', array(10, 'penobit'));

QB::table('my_table')
            ->select(QB::raw('count(cb_my_table.id) as tot'))
            ->where('value', '=', 'Ifrah')
            ->where(QB::raw('DATE(?)', 'now'))

$data = array(
    'name' => 'Sana',
    'description' => 'Blah'
);
$insertId = QB::table('my_table')->insert($data);

$data = array(
    array(
        'name'        => 'Sana',
        'description' => 'Blah'
    ),
    array(
        'name'        => 'Penobit',
        'description' => 'Blah'
    ),
);
$insertIds = QB::table('my_table')->insert($data);

$data = array(
    'name'    => 'Sana',
    'counter' => 1
);
$dataUpdate = array(
    'name'    => 'Sana',
    'counter' => 2
);
$insertId = QB::table('my_table')->onDuplicateKeyUpdate($dataUpdate)->insert($data);

$data = array(
    'name'        => 'Sana',
    'description' => 'Blah'
);

QB::table('my_table')->where('id', 5)->update($data);

QB::table('my_table')->where('id', '>', 5)->delete();

QB::transaction(function ($qb) {
    $qb->table('my_table')->insert(array(
        'name' => 'Test',
        'url' => 'example.com'
    ));

    $qb->table('my_table')->insert(array(
        'name' => 'Test2',
        'url' => 'example.com'
    ));
});

QB::transaction(function ($qb) {
    $qb->table('my_table')->insert(array(/* data... */));

    $qb->commit(); // to commit the changes (data would be saved)
    $qb->rollback(); // to rollback the changes (data would be rejected)
});

$query = QB::table('my_table')->where('id', '=', 3);
$queryObj = $query->getQuery();

$queryObj->getSql();
// Returns: SELECT * FROM my_table where `id` = ?

$queryObj->getBindings();
// Returns: array(3)

$queryObj->getRawSql();
// Returns: SELECT * FROM my_table where `id` = 3

$subQuery = QB::table('person_details')->select('details')->where('person_id', '=', 3);


$query = QB::table('my_table')
            ->select('my_table.*')
            ->select(QB::subQuery($subQuery, 'table_alias1'));

$nestedQuery = QB::table(QB::subQuery($query, 'table_alias2'))->select('*');
$nestedQuery->get();

QB::pdo();

QB::table('my_table')->asObject('SomeClass', array('ctor', 'args'))->first();

QB::table('my_table')->setFetchMode(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE)->get();

QB::registerEvent('before-select', 'users', function($qb)
{
    $qb->where('status', '!=', 'banned');
});

QB::registerEvent('after-insert', 'my_table', function($queryBuilder, $insertId)
{
    $data = array('person_id' => $insertId, 'details' => 'Meh', 'age' => 5);
    $queryBuilder->table('person_details')->insert($data);
});

QB::registerEvent('after-insert', 'person_details', function($queryBuilder, $insertId)
{
    $queryBuilder->table('person_details')->where('id', $insertId)->update(array('created_at' => date('Y-m-d H:i:s')));
});

QB::registerEvent('after-delete', 'my_table', function($queryBuilder, $queryObject)
{
    $bindings = $queryObject->getBindings();
    $queryBuilder->table('person_details')->where('person_id', $binding[0])->delete();
});

QB::removeEvent('event-name', 'table-name');