QB
in component
This 'Query Builder' class is used for building SELECT queries from parts and executing them later.
Here is how a simple SQL query looks in Opencart:
$query = $this->query("SELECT * FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
$product = $query->row;
This is the same query rewritten with QB class:
$qb = new \extension\ka_extensions\QB();
$qb->select("*", "product");
$qb->where("product_id", $product_id);
$product = $qb->query();
The main goal of this class is to separate building the request from executing it. That allows developers to build the request in a separate function like this:
class Product extends Model {
public function getProduct($product_id) {
$qb = $this->getProductQB(['product_id' => $product_id]);
$product = $qb->query()->row;
return $product;
}
//
// This function makes QB object. These functions for preparing QB class have 'protected' visibility.
//
protected function getProductQB($data) {
$qb = new \extension\ka_extensions\QB();
$qb->select("*", "product", "p");
$qb->where("p.product_id", $product_id);
return $qb;
}
}
When QB is prepared in a separate function, it be can adjusted in a child class. 'Ka Extensions' library allows developers to inherate existing classes in their modules. Also it makes simpler to position ocmod/vqmod patches to sql queries. So, it is better to build SQL queries with QB class than writing raw SQL in the code.
Table of Contents
Properties
Methods
- __construct() : mixed
- delete() : mixed
- from() : mixed
- PARAMS
$from - table name
$from_key - table alias - fromUnionAll() : mixed
- PARAMS
$from - table name
$from_key - table alias - getSql() : mixed
- groupBy() : mixed
- innerJoin() : mixed
- leftJoin() : mixed
- limit() : mixed
- orderBy() : mixed
- query() : mixed
- Builds and runs the query from the QB data.
- select() : mixed
- where() : mixed
Properties
$delete
public
mixed
$delete
= array()
$from
public
mixed
$from
= array()
$from_union
public
mixed
$from_union
= []
$from_union_alias
public
mixed
$from_union_alias
= 't'
$groupBy
public
mixed
$groupBy
= array()
$innerJoin
public
mixed
$innerJoin
= array()
$leftJoin
public
mixed
$leftJoin
= array()
$limit
public
mixed
$limit
= array()
$orderBy
public
mixed
$orderBy
= array()
$select
public
mixed
$select
= array()
$where
public
mixed
$where
= array()
Methods
__construct()
public
__construct() : mixed
delete()
public
delete(mixed $what[, mixed $from = '' ][, mixed $from_key = '' ]) : mixed
Parameters
- $what : mixed
- $from : mixed = ''
- $from_key : mixed = ''
from()
PARAMS $from - table name $from_key - table alias
public
from(mixed $from[, mixed $from_key = '' ]) : mixed
Parameters
- $from : mixed
- $from_key : mixed = ''
fromUnionAll()
PARAMS $from - table name $from_key - table alias
public
fromUnionAll(mixed $from[, mixed $alias = null ]) : mixed
Parameters
- $from : mixed
- $alias : mixed = null
getSql()
public
getSql() : mixed
groupBy()
public
groupBy(mixed $groupBy[, mixed $after = '' ]) : mixed
Parameters
- $groupBy : mixed
- $after : mixed = ''
innerJoin()
public
innerJoin(mixed $from[, mixed $from_key = '' ][, mixed $condition = '' ]) : mixed
Parameters
- $from : mixed
- $from_key : mixed = ''
- $condition : mixed = ''
leftJoin()
public
leftJoin(mixed $from[, mixed $from_key = '' ][, mixed $condition = '' ]) : mixed
Parameters
- $from : mixed
- $from_key : mixed = ''
- $condition : mixed = ''
limit()
public
limit(mixed $start, mixed $limit) : mixed
Parameters
- $start : mixed
- $limit : mixed
orderBy()
public
orderBy(mixed $order[, mixed $after = '' ]) : mixed
Parameters
- $order : mixed
- $after : mixed = ''
query()
Builds and runs the query from the QB data.
public
query() : mixed
Returns a standard 'sql result' object with $row, $rows and other fields.
select()
public
select(mixed $what[, mixed $from = '' ][, mixed $from_key = '' ]) : mixed
Parameters
- $what : mixed
- $from : mixed = ''
- $from_key : mixed = ''
where()
public
where(mixed $where[, mixed $value = null ]) : mixed
Parameters
- $where : mixed
- $value : mixed = null