Getting started
The following guide will help you install the Query Builder package inside your WordPress plugin and start using it. The guide assumes you have a basic understanding of PHP and WordPress plugin development and have Composer installed on your system.
This library implements an abstraction layer over the wpdb
class and allows you to build and execute database queries through a fluent interface.
TIP
The Query Builder package is designed to work independently from the framework. You can use this package in any WordPress plugin or project without the need to use the framework.
Installation
To install the Query Builder package, you need to add it as a dependency to your plugin. Open a terminal window and navigate to your plugin directory.
Run the following command to add the package:
composer require sematico/baselibs-query-builder --prefer-dist --dev
TIP
The --dev
flag is used because we assume you are scoping your plugin via PHP Scoper.
Basic usage
Interaction with the database is achieved with the help of the QueryBuilder
class, which provides various methods that will ease the process of manipulating and fetching records from the database.
The class provides methods for selecting, inserting, updating, and deleting records. It also provides methods for chaining together multiple operations, making it easy to build complex queries.
Selecting records
To select records from the database, you can use the select
method. This method accepts a string or an array of column names to be selected.
use Sematico\Baselibs\QueryBuilder\QueryBuilder;
$query = new QueryBuilder();
$select = $query_builder->select()
->from( 'users' )
->limit( 10 );
$records = $select->fetch_all();
TIP
You do not need to enter the database prefix when using the query builder.
Inserting records
To insert a record into the database, you can use the insert
method. This method accepts an array of column names and values to be inserted.
$query = new QueryBuilder();
$insert = $query_builder->insert(
[
'column1' => 'value1',
'column2' => 'value2',
]
);
After that, you can specify the table to insert into using the into method:
$result = $insert->into('table_name');
The into
method will automatically execute the insert query and return the number of affected rows (or false on error).
Updating records
You can use the update
method to create an Update
statement for modifying existing records in a database table.
$update = $query_builder->update(
[
'name' => 'John',
'email' => '[email protected]',
]
)
->table( 'users' )
->where( 'id' )->is( 1 );
The where
method is used to specify the condition that must be met for the update to be executed. In this case, the update will only be executed for the record with an ID
of 1
.
You may use the into
method as it combines specifying the table and executing the update in one step without the need to call the execute
method and without the need to set the table name.
$result = $update->into( 'users' );
Deleting records
You can use the delete
method to create a Delete
statement for removing records from a database table.
$delete = $query_builder->delete()
->from( 'users' )
->where( 'id' )->is( 1 );
The where
method is used to specify the condition that must be met for the delete to be executed. In this case, the delete will only be executed for the record with an ID
of 1
.
You must then execute the query using the execute
method:
$result = $delete->execute();
The execute
method will execute the delete query and return the number of affected rows (or false on error).
Source code & changelog
The source code and changelog for the Query Builder package can be found on GitHub.