class QueryBuilder (View source)

QueryBuilder class

Properties

static protected object $instance
protected DoliDb $db
protected bool|null $result

Methods

__construct()

Constructor

static 
getInstance()

Return Query Builder instance

string
get()

Return query

reset()

Reset Query Builder

bool|null
execute()

Execute query

array
result($limit)

Execute query if not executed & return an array of result(s)

int|null
count()

Execute query if not executed & return query result(s) count

int|null
affected()

Return affected rows count of an INSERT, UPDATE or DELETE query

int|null
lastId($table_name, $pk_name)

Return last id after an INSERT query

string
escape($value)

Escape field value

$this
select($select_options = '*', $distinct = false, $table_alias = '')

Add SELECT statement to query

$this
addSelect($select_options, $table_alias = '')

Add more options to SELECT statement (multiple calls allowed)

$this
from($table_name, $table_alias = '')

Add FROM clause to query

$this
where($where_options)

Add WHERE clause to query (multiple calls allowed)

$this
orWhere($where_options)

Add OR to WHERE clause (multiple calls allowed)

$this
groupBy($group_options)

Add GROUP BY clause to query

$this
orderBy($order_options, $order = 'ASC')

Add ORDER BY clause to query

$this
limit($limit, $offset)

Add LIMIT clause to query

$this
join($table_name, $join_options, $join_type = '')

Add JOIN clause to query (multiple calls allowed)

$this
insert($table_name, $data)

Add INSERT statement to query

$this
update($table_name, $data)

Add UPDATE statement to query

$this
delete($table_name)

Add DELETE statement to query

$this
truncate($table_name)

Add TRUNCATE statement to query

Details

__construct()

Constructor

static getInstance()

Return Query Builder instance

string get()

Return query

Return Value

string query

reset()

Reset Query Builder

bool|null execute()

Execute query

Return Value

bool|null query execution result

array result($limit)

Execute query if not executed & return an array of result(s)

Parameters

$limit result limit

Return Value

array query result rows

int|null count()

Execute query if not executed & return query result(s) count

Return Value

int|null query result rows count

int|null affected()

Return affected rows count of an INSERT, UPDATE or DELETE query

Return Value

int|null query affected rows count

int|null lastId($table_name, $pk_name)

Return last id after an INSERT query

Parameters

$table_name table name
$pk_name primary key/id field name

Return Value

int|null id or -1 on error

protected string escape($value)

Escape field value

Parameters

$value field value

Return Value

string escaped value

$this select($select_options = '*', $distinct = false, $table_alias = '')

Add SELECT statement to query

Parameters

$select_options select options string or array
$distinct use DISTINCT or not
$table_alias table alias (works only when $select_options is an array)

Return Value

$this

$this addSelect($select_options, $table_alias = '')

Add more options to SELECT statement (multiple calls allowed)

Parameters

$select_options select options string or array
$table_alias table alias (works only when $select_options is an array)

Return Value

$this

$this from($table_name, $table_alias = '')

Add FROM clause to query

Parameters

$table_name table name string or array
$table_alias table alias (works only when $table_name is a string)

Return Value

$this

$this where($where_options)

Add WHERE clause to query (multiple calls allowed)

Parameters

$where_options where options string or array

Return Value

$this

$this orWhere($where_options)

Add OR to WHERE clause (multiple calls allowed)

Parameters

$where_options where options string or array

Return Value

$this

$this groupBy($group_options)

Add GROUP BY clause to query

Parameters

$group_options group options string

Return Value

$this

$this orderBy($order_options, $order = 'ASC')

Add ORDER BY clause to query

Parameters

$order_options order options string
$order order 'ASC' or 'DESC', default: 'ASC'

Return Value

$this

$this limit($limit, $offset)

Add LIMIT clause to query

Parameters

$limit limit integer
$offset offset integer

Return Value

$this

$this join($table_name, $join_options, $join_type = '')

Add JOIN clause to query (multiple calls allowed)

Parameters

$table_name table name
$join_options join options string
$join_type join type 'left', 'right', 'inner', 'outer'

Return Value

$this

$this insert($table_name, $data)

Add INSERT statement to query

Parameters

$table_name table name
$data data array ['key' => 'value']

Return Value

$this

$this update($table_name, $data)

Add UPDATE statement to query

Parameters

$table_name table name
$data data array ['key' => 'value']

Return Value

$this

$this delete($table_name)

Add DELETE statement to query

Parameters

$table_name table name

Return Value

$this

$this truncate($table_name)

Add TRUNCATE statement to query

Parameters

$table_name table name

Return Value

$this