MilkCore

Query
in package

SQL Query Builder Class

This class provides a fluent interface for building SQL queries, allowing you to specify components like SELECT, WHERE, ORDER BY, and LIMIT in a simple and intuitive way.

Tags
example
// Basic query example
$query = new Query('users');
$query->select(['id', 'username', 'email'])
      ->where(['status = ?'], [1])
      ->order('created_at', 'DESC')
      ->limit(0, 10);
list($sql, $params) = $query->get();

// Join example
$query = new Query('orders');
$query->select(['orders.id', 'orders.total', 'users.name'])
      ->from('LEFT JOIN users ON users.id = orders.user_id')
      ->where(['orders.status = ?'], ['completed'])
      ->get();

Table of Contents

Properties

$db  : MySql
Database connection instance
$db_type  : string
Database type
$from  : array<string|int, mixed>
Additional FROM or JOIN clauses
$group  : string
GROUP BY clause for the query
$having  : array<string|int, mixed>
HAVING conditions for the query
$limit  : array<string|int, mixed>
LIMIT clause for the query
$order  : array<string|int, mixed>
ORDER BY clauses for the query
$select  : array<string|int, mixed>|string
List of fields to select
$table  : string
The table name for the query
$where  : array<string|int, mixed>
WHERE conditions for the query

Methods

__construct()  : mixed
Query class constructor
clean()  : mixed
Pulisce i parametri della query.
from()  : $this
Specifies additional FROM or JOIN clauses
get()  : array<string|int, mixed>
Costruisce e ritorna la query SQL e i parametri da passare al bind_param.
get_total()  : array<string|int, mixed>
Costruisce e ritorna la query SQL per calcolare il numero totale di record.
group()  : mixed
Imposta il gruppo di risultati.
has_group()  : mixed
Verifica se ha un gruppo
has_having()  : mixed
Verifica se ha uno o più parametri having
has_limit()  : mixed
Verifica se ha un limite
has_order()  : mixed
has_select()  : bool
Checks if any fields have been selected
has_where()  : mixed
Verifica se ha uno o più parametri where
having()  : $this
Aggiunge condizioni HAVING alla query.
limit()  : $this
Imposta il limite di risultati da restituire.
order()  : $this
Specifica l'ordinamento dei risultati.
select()  : $this
Specifies the fields to select
where()  : $this
Aggiunge condizioni WHERE alla query.

Properties

$from

Additional FROM or JOIN clauses

public array<string|int, mixed> $from = []

Contains additional tables to join in the query.

$group

GROUP BY clause for the query

public string $group = ''

Groups the query results by specified fields.

$having

HAVING conditions for the query

public array<string|int, mixed> $having = []

Used with GROUP BY to filter grouped results.

$limit

LIMIT clause for the query

public array<string|int, mixed> $limit = []

Limits the number of results returned by the query.

$order

ORDER BY clauses for the query

public array<string|int, mixed> $order = []

Specifies the sorting order of the query results.

$select

List of fields to select

public array<string|int, mixed>|string $select = []

Can be an array of field names or a string with comma-separated field names.

$table

The table name for the query

public string $table = ''

$where

WHERE conditions for the query

public array<string|int, mixed> $where = []

Contains the conditions to filter the query results.

Methods

__construct()

Query class constructor

public __construct(string $table[, MySql|null $db = null ]) : mixed

Initializes a new Query instance for the specified table.

Parameters
$table : string

The table name to build the query on

$db : MySql|null = null

Optional custom database connection

Tags
example
// Basic initialization
$query = new Query('users');

// With custom database connection
$customDb = new MySql($host, $user, $password, $database);
$query = new Query('users', $customDb);

clean()

Pulisce i parametri della query.

public clean([string $single = '' ]) : mixed
Parameters
$single : string = ''

Se una stringa "select" o "from" o "where" o "order" o "limit" viene passata come parametro, verrà pulito solo quel parametro.

from()

Specifies additional FROM or JOIN clauses

public from(string $from[, bool $clear = false ]) : $this

Adds JOIN statements to the query. The method automatically detects JOIN types (INNER, LEFT, RIGHT, etc.) from the provided string.

Parameters
$from : string

JOIN statement to add

$clear : bool = false

Whether to clear previous JOINs (default: false)

Tags
example
// Basic join
$query->from('JOIN categories ON products.category_id = categories.id');

// Left join
$query->from('LEFT JOIN users ON orders.user_id = users.id');

// Multiple joins
$query->from('JOIN users ON orders.user_id = users.id')
      ->from('LEFT JOIN products ON order_items.product_id = products.id');
Return values
$this

Allows method chaining

get()

Costruisce e ritorna la query SQL e i parametri da passare al bind_param.

public get([mixed $convert = true ]) : array<string|int, mixed>
Parameters
$convert : mixed = true
Return values
array<string|int, mixed>

Array contenente la stringa SQL e i parametri per il bind_param.

get_total()

Costruisce e ritorna la query SQL per calcolare il numero totale di record.

public get_total() : array<string|int, mixed>
Return values
array<string|int, mixed>

Array contenente la stringa SQL e i parametri per il bind_param.

group()

Imposta il gruppo di risultati.

public group(string $group) : mixed
Parameters
$group : string

has_group()

Verifica se ha un gruppo

public has_group() : mixed

has_having()

Verifica se ha uno o più parametri having

public has_having() : mixed

has_limit()

Verifica se ha un limite

public has_limit() : mixed

has_select()

Checks if any fields have been selected

public has_select() : bool
Return values
bool

True if fields have been selected, false otherwise

has_where()

Verifica se ha uno o più parametri where

public has_where() : mixed

having()

Aggiunge condizioni HAVING alla query.

public having(string|array<string|int, mixed> $having[, array<string|int, mixed> $params = [] ][, string $operator = 'AND' ]) : $this
Parameters
$having : string|array<string|int, mixed>

Condizioni HAVING es. 'COUNT(*) > ?'

$params : array<string|int, mixed> = []

Parametri da passare al bind_param. es. ['10']

$operator : string = 'AND'

Operatore logico da utilizzare ('AND' o 'OR').

Return values
$this

Permette il chaining dei metodi.

limit()

Imposta il limite di risultati da restituire.

public limit(int $start, int $limit) : $this
Parameters
$start : int

Primo record da selezionare.

$limit : int

Numero di record da selezionare.

Return values
$this

Permette il chaining dei metodi.

order()

Specifica l'ordinamento dei risultati.

public order([string|array<string|int, mixed> $field = '' ][, string|array<string|int, mixed> $dir = 'asc' ][, mixed $clear = true ]) : $this
Parameters
$field : string|array<string|int, mixed> = ''

Campo o array di campi su cui ordinare.

$dir : string|array<string|int, mixed> = 'asc'

Direzione dell'ordinamento ('asc' o 'desc') per campo singolo, o array di direzioni per campi multipli.

$clear : mixed = true
Return values
$this

Permette il chaining dei metodi.

select()

Specifies the fields to select

public select(string|array<string|int, mixed> $fields[, bool $clear = true ]) : $this

Sets the fields to be selected in the query. Fields can be specified as an array or as a comma-separated string.

Parameters
$fields : string|array<string|int, mixed>

Fields to select

$clear : bool = true

Whether to clear previous selections (default: true)

Tags
example
// Select specific fields
$query->select(['id', 'username', 'email']);

// Select with aliases
$query->select(['u.id', 'u.username AS user', 'COUNT(*) AS total']);

// Using a string
$query->select('id, username, email');
Return values
$this

Allows method chaining

where()

Aggiunge condizioni WHERE alla query.

public where(mixed $where[, array<string|int, mixed> $params = [] ][, string $operator = 'AND' ]) : $this
Parameters
$where : mixed
$params : array<string|int, mixed> = []

Parametri da passare al bind_param. es. ['valore1', 'valore2']

$operator : string = 'AND'

Operatore logico da utilizzare ('AND' o 'OR').

Tags
todo
Return values
$this

Permette il chaining dei metodi.


        
On this page

Search results