NinjaDB
A lightweight, expressive query builder for WordPress. NInjaDB use the same $wbdp instance and methods, It will help you to write $wpdb queries easily and expressively. At least PHP 5.3 is required. It has some advanced features like: – Query Like Laravel Basic Queries – Where, orWhere methods – Sea
About this tool.
A lightweight, expressive query builder for WordPress. NInjaDB use the same $wbdp instance and methods, It will help you to write $wpdb queries easily and expressively. At least PHP 5.3 is required.
It has some advanced features like: – Query Like Laravel Basic Queries – Where, orWhere methods – Search data in all the columns or specific columns – Easily Implement Pagination – use insert, update or delete methods expressively.
The syntax is quite similar to Laravel’s query builder.
Simple Query:
The query below returns the row where id = 3, null if no rows.
$row = ninjaDB('my_table')->find(3);
Full Queries:
$query = ninjaDB('my_table')->where('post_author', '=', 1); // for equal you can just use where('post_author, 1); // Get result as array of objects $query->get();
Full Usage API
### Initiate
// Select a table $query = ninjaDB()->table('post'); // post is the table name without prefix;
OR You can pass your table name as an argument
// Select a table $query = ninjaDB('my_table'); // post is the table name without prefix;<h3>Query</h3>
Get Easily
The query below returns the (first) row where id = 3, null if no rows.
$row = ninjaDB('my_table')->find(3);
Access your row like, echo $row->name . If your field name is not id then pass the field name as second parameter ninjaDB('my_table')->find(3, 'author_id'); .
The query below returns the all rows as array of objects where author_id = 3, null if no rows.
$result = ninjaDB('my_table')->findAll('author_id', 3);
Select
$query = ninjaDB('my_table')->select('*');
Multiple Selects
->select(array('myfield1', 'myfield2', 'amyfield3'));
Using select method multiple times select('a')->select('b') will also select a and b . Can be useful if you want to do conditional selects (within a PHP if ).
Get All
Return an array of objects.
$query = ninjaDB('my_table')->where('author_id', 1); $result = $query->get();
You can loop through it like:
foreach ($result as $row) { echo $row->name; }
Get First Row
$query = ninjaDB('my_table')->where('author_id', 1); $row = $query->first();
Returns the first row, or null if there is no record. Using this method you can also make sure if a record exists. Access these like echo $row->name .
Get Rows Count, MAX, MIN, AVerage, SUM
$query = ninjaDB('my_table')->where('author_id', 1); $count = $query->count(); $max = $query->max('views'); // Where `views` is the column name and all these will return integer / float $min = $query->min('views'); $avg = $query->avg('views'); $avg = $query->avg('views'); $sum = $query->sum('views');