PHP code example of kaizencoders / wp-fluent

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

    

kaizencoders / wp-fluent example snippets


// You can use the global wpFluent() function
$user = wpFluent()->table('users')->find(1);

// Or, create a connection using $wpdb, only once.
global $wpdb;
new \WpFluent\Connection($wpdb, ['prefix' => $wpdb->prefix], 'DB');

$user = DB::table('users')->find(3);

$query = DB::table('users')->where('display_name', 'LIKE', '%admin%');

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

DB::registerEvent('before-select', 'posts', function ($qb) {
    $qb->where('psot_status', '!=', 'draft');
});

// Get the global wpdb instance.
global $wpdb;

new \WpFluent\Connection($wpdb, ['prefix' => $wpdb->prefix], 'DB');

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

// Or, simply use the global wpFluent() function.
// It handles all the ncessary initial setup.
wpFluent()->table('my_table')->where('name', '=', 'admin');

new \WpFluent\Connection($wpdb, ['prefix' => $wpdb->prefix], 'MyAlias');

$connection = new \WpFluent\Connection($wpdb, ['prefix' => $wpdb->prefix]);

$db = new \WpFluent\QueryBuilder\QueryBuilderHandler($connection);

$query = $db->table('my_table')->where('name', '=', 'admin');

var_dump($query->get());

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

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

$result = DB::table('my_table')->findAll('name', 'admin');

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

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

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

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

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

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

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

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

DB::table('my_table')
    ->whereIn('name', array('rabindranath', 'najrul'))
    ->orWhereIn('name', array('homer', 'frost'));

DB::table('my_table')
    ->whereNotIn('name', array('homer', 'frost'))
    ->orWhereNotIn('name', array('rabindranath', 'najrul'));

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

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

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

$query = DB::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);

DB::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', '>', DB::raw(1));
})

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

var_dump($query->get());

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

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

$data = [
    'name'        => 'Najrul',
    'description' => 'Famous Bengali poet.'
];

$insertId = DB::table('my_table')->insert($data);

$data = array(
    [
        'name'        => 'Najrul',
        'description' => 'Famous Bengali poet.'
    ],
    [
        'name'        => 'Rabindranath',
        'description' => 'Nobel winning Bengali poet.'
    ],
);

$insertIds = DB::table('my_table')->insert($data);

$data = [
    'name'    => 'Najrul',
    'counter' => 1
];

$dataUpdate = [
    'name'    => 'Najrul',
    'counter' => 2
];

$insertId = DB::table('my_table')->onDuplicateKeyUpdate($dataUpdate)->insert($data);

$data = [
    'name'        => 'Najrul',
    'description' => 'Famous Bengali poet.'
];

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

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

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

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

DB::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 = DB::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 = DB::table('person_details')->select('details')->where('person_id', '=', 3);

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

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

DB::db();

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

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

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

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

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