PHP Class 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(); }
Author: Anton Shevchuk
Show file Open project: bluzphp/framework Class Usage Examples

Protected Properties

Property Type Description
$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

Public Methods

Method Description
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

Private Methods

Method Description
__construct ( ) Create and initialize Table instance
prepareStatement ( array $where ) : array Prepare array for WHERE or SET statements

Method Details

create() public static method

Create Row instance
public static create ( array $data = [] ) : Row
$data array
return Row

delete() public static method

Table::delete(['login' => 'Man'])
public static delete ( array $where ) : integer
$where array An array of SQL WHERE clause(s)
return integer The number of rows deleted

fetch() public static method

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
return array of rows results in FETCH_CLASS mode

fetchAll() public static method

Fetch all rows from table Be carefully with this method, can be very slow
public static fetchAll ( ) : array
return array of rows results in FETCH_CLASS mode

filterColumns() public static method

Filter columns for insert/update queries by table columns definition
public static filterColumns ( array $data ) : array
$data array
return array

find() public static method

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.
return array

findRow() public static method

Find row by primary key
public static findRow ( mixed $primaryKey ) : Row
$primaryKey mixed
return Row

findRowWhere() public static method

Find row by where condition
public static findRowWhere ( array $whereList ) : Row
$whereList array
return Row

findWhere() public static method

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
return array

getColumns() public method

Return information about tables columns
public getColumns ( ) : array
return array

getInstance() public static method

Get Table instance
public static getInstance ( ) : static
return static

getModel() public method

Get model name
public getModel ( ) : string
return string

getName() public method

Get table name
public getName ( ) : string
return string

getPrimaryKey() public method

Get primary key(s)
public getPrimaryKey ( ) : array
return array

getSelectQuery() public method

Get select query
public getSelectQuery ( ) : string
return string

init() public method

Subclasses may override this method
public init ( )

insert() public static method

Table::insert(['login' => 'Man', 'email' => '[email protected]'])
public static insert ( array $data ) : string | null
$data array Column-value pairs
return string | null Primary key or null

linkTo() public method

Setup relation "one to one" or "one to many"
public linkTo ( string $key, string $model, string $foreign ) : void
$key string
$model string
$foreign string
return void

linkToMany() public method

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
return void

select() public static method

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
return Bluz\Db\Query\Select

setSelectQuery() public method

Set select query
public setSelectQuery ( string $select ) : Table
$select string SQL query
return Table

update() public static method

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)
return integer The number of rows updated

Property Details

$columns protected property

table columns
protected $columns

$model protected property

the model name
protected $model

$primary protected property

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

$rowClass protected property

row class name
protected $rowClass

$select protected property

default SQL query for select
protected $select

$sequence protected property

the sequence name, required for PostgreSQL
protected $sequence

$table protected property

the table name
protected $table