PHP Класс Bluz\Db\Table

Example of Users\Table namespace Application\Users; class Table extends \Bluz\Db\Table { protected $table = 'users'; protected $primary = array('id'); } $userRows = \Application\Users\Table::find(1,2,3,4,5); foreach ($userRows as $userRow) { $userRow -> description = 'In first 5'; $userRow -> save(); }
Автор: Anton Shevchuk
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$columns table columns
$model the model name
$primary the primary key column or columns (only as array).
$rowClass row class name
$select default SQL query for select
$sequence the sequence name, required for PostgreSQL
$table the table name

Открытые методы

Метод Описание
create ( array $data = [] ) : Row Create Row instance
delete ( array $where ) : integer Deletes existing rows
fetch ( string $sql, array $params = [] ) : array Fetching rows by SQL query
fetchAll ( ) : array Fetch all rows from table Be carefully with this method, can be very slow
filterColumns ( array $data ) : array Filter columns for insert/update queries by table columns definition
find ( $keys ) : array Fetches rows by primary key. The argument specifies one or more primary key value(s). To find multiple rows by primary key, the argument must be an array.
findRow ( mixed $primaryKey ) : Row Find row by primary key
findRowWhere ( array $whereList ) : Row Find row by where condition
findWhere ( $where ) : array Find rows by WHERE WHERE alias = 'foo' Table::findWhere(['alias'=>'foo']); WHERE alias = 'foo' OR 'alias' = 'bar' Table::findWhere(['alias'=>'foo'], ['alias'=>'bar']); WHERE (alias = 'foo' AND userId = 2) OR ('alias' = 'bar' AND userId = 4) Table::findWhere(['alias'=>'foo', 'userId'=> 2], ['alias'=>'foo', 'userId'=>4]); WHERE alias IN ('foo', 'bar') Table::findWhere(['alias'=> ['foo', 'bar']]);
getColumns ( ) : array Return information about tables columns
getInstance ( ) : static Get Table instance
getModel ( ) : string Get model name
getName ( ) : string Get table name
getPrimaryKey ( ) : array Get primary key(s)
getSelectQuery ( ) : string Get select query
init ( ) Initialization hook.
insert ( array $data ) : string | null Insert new record to table and return last insert Id
linkTo ( string $key, string $model, string $foreign ) : void Setup relation "one to one" or "one to many"
linkToMany ( string $model, string $link ) : void Setup relation "many to many" [table1-key] [table1_key-table2-table3_key] [table3-key]
select ( ) : Select Prepare Db\Query\Select for current table: - predefine "select" section as "*" from current table - predefine "from" section as current table name and first letter as alias - predefine fetch type
setSelectQuery ( string $select ) : Table Set select query
update ( array $data, array $where ) : integer Updates existing rows

Приватные методы

Метод Описание
__construct ( ) Create and initialize Table instance
prepareStatement ( array $where ) : array Prepare array for WHERE or SET statements

Описание методов

create() публичный статический Метод

Create Row instance
public static create ( array $data = [] ) : Row
$data array
Результат Row

delete() публичный статический Метод

Table::delete(['login' => 'Man'])
public static delete ( array $where ) : integer
$where array An array of SQL WHERE clause(s)
Результат integer The number of rows deleted

fetch() публичный статический Метод

Fetching rows by SQL query
public static fetch ( string $sql, array $params = [] ) : array
$sql string SQL query with placeholders
$params array Params for query placeholders
Результат array of rows results in FETCH_CLASS mode

fetchAll() публичный статический Метод

Fetch all rows from table Be carefully with this method, can be very slow
public static fetchAll ( ) : array
Результат array of rows results in FETCH_CLASS mode

filterColumns() публичный статический Метод

Filter columns for insert/update queries by table columns definition
public static filterColumns ( array $data ) : array
$data array
Результат array

find() публичный статический Метод

This method accepts a variable number of arguments. If the table has a multi-column primary key, the number of arguments must be the same as the number of columns in the primary key. To find multiple rows in a table with a multi-column primary key, each argument must be an array with the same number of elements. The find() method always returns a array Row by primary key, return array Table::find(123); Row by compound primary key, return array Table::find([123, 'abc']); Multiple rows by primary key Table::find(123, 234, 345); Multiple rows by compound primary key Table::find([123, 'abc'], [234, 'def'], [345, 'ghi'])
public static find ( $keys ) : array
$keys The value(s) of the primary keys.
Результат array

findRow() публичный статический Метод

Find row by primary key
public static findRow ( mixed $primaryKey ) : Row
$primaryKey mixed
Результат Row

findRowWhere() публичный статический Метод

Find row by where condition
public static findRowWhere ( array $whereList ) : Row
$whereList array
Результат Row

findWhere() публичный статический Метод

Find rows by WHERE WHERE alias = 'foo' Table::findWhere(['alias'=>'foo']); WHERE alias = 'foo' OR 'alias' = 'bar' Table::findWhere(['alias'=>'foo'], ['alias'=>'bar']); WHERE (alias = 'foo' AND userId = 2) OR ('alias' = 'bar' AND userId = 4) Table::findWhere(['alias'=>'foo', 'userId'=> 2], ['alias'=>'foo', 'userId'=>4]); WHERE alias IN ('foo', 'bar') Table::findWhere(['alias'=> ['foo', 'bar']]);
public static findWhere ( $where ) : array
$where
Результат array

getColumns() публичный Метод

Return information about tables columns
public getColumns ( ) : array
Результат array

getInstance() публичный статический Метод

Get Table instance
public static getInstance ( ) : static
Результат static

getModel() публичный Метод

Get model name
public getModel ( ) : string
Результат string

getName() публичный Метод

Get table name
public getName ( ) : string
Результат string

getPrimaryKey() публичный Метод

Get primary key(s)
public getPrimaryKey ( ) : array
Результат array

getSelectQuery() публичный Метод

Get select query
public getSelectQuery ( ) : string
Результат string

init() публичный Метод

Subclasses may override this method
public init ( )

insert() публичный статический Метод

Table::insert(['login' => 'Man', 'email' => '[email protected]'])
public static insert ( array $data ) : string | null
$data array Column-value pairs
Результат string | null Primary key or null

linkTo() публичный Метод

Setup relation "one to one" or "one to many"
public linkTo ( string $key, string $model, string $foreign ) : void
$key string
$model string
$foreign string
Результат void

linkToMany() публичный Метод

Setup relation "many to many" [table1-key] [table1_key-table2-table3_key] [table3-key]
public linkToMany ( string $model, string $link ) : void
$model string
$link string
Результат void

select() публичный статический Метод

use default select "*" $select = Users\Table::select(); $arrUsers = $select->where('u.id = ?', $id) ->execute(); setup custom select "u.id, u.login" $select = Users\Table::select(); $arrUsers = $select->select('u.id, u.login') ->where('u.id = ?', $id) ->execute();
public static select ( ) : Select
Результат Bluz\Db\Query\Select

setSelectQuery() публичный Метод

Set select query
public setSelectQuery ( string $select ) : Table
$select string SQL query
Результат Table

update() публичный статический Метод

Table::insert(['login' => 'Man', 'email' => '[email protected]'], ['id' => 42])
public static update ( array $data, array $where ) : integer
$data array Column-value pairs.
$where array An array of SQL WHERE clause(s)
Результат integer The number of rows updated

Описание свойств

$columns защищенное свойство

table columns
protected $columns

$model защищенное свойство

the model name
protected $model

$primary защищенное свойство

the primary key column or columns (only as array).
protected $primary

$rowClass защищенное свойство

row class name
protected $rowClass

$select защищенное свойство

default SQL query for select
protected $select

$sequence защищенное свойство

the sequence name, required for PostgreSQL
protected $sequence

$table защищенное свойство

the table name
protected $table