class Selection implements Iterator, IRowContainer, ArrayAccess, Countable

Filtered table representation.

Selection is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.

Traits

Strict class for better experience.

Properties

protected Context $context
protected IConventions $conventions
protected ColumnAccessCache $cache
protected ReferenceCache $refCache
protected array $globalRefCache
protected SqlBuilder $sqlBuilder
protected string $name
protected string|array|null $primary
protected string|bool $primarySequence
protected IRow[] $rows
protected IRow[] $data
protected bool $dataRefreshed
protected array $aggregation
protected array $keys

Methods

__construct(Context $context, IConventions $conventions, string $tableName, IStorage $cacheStorage = null)

Creates filtered table representation.

__destruct()

No description

__clone()

No description

string
getName()

No description

string|array|null
getPrimary(bool $throw = true)

No description

string|null
getPrimarySequence()

No description

setPrimarySequence(string $sequence)

No description

string
getSql()

No description

getSqlBuilder()

No description

getCache()

No description

getGlobalRefCache(string $key)

No description

getConnection()

No description

ActiveRow|null
get(mixed $key)

Returns row specified by primary key.

IRow|null
fetch()

Fetches single row object.

mixed
fetchField(string $column = null) deprecated

Fetches single field.

array
fetchPairs(string|int $key = null, string|int $value = null)

Fetches all rows as associative array.

array
fetchAll()

Fetches all rows.

array
fetchAssoc(string $path)

Fetches all rows and returns associative tree.

select($columns, ...$params)

Adds select clause, more calls appends to the end.

wherePrimary(mixed $key)

Adds condition for primary key.

where($condition, ...$params)

Adds where condition, more calls appends with AND.

joinWhere(string $tableChain, string $condition, ...$params)

Adds ON condition when joining specified table, more calls appends with AND.

void
condition($condition, array $params, $tableChain = null)

Adds condition, more calls appends with AND.

whereOr(array $parameters)

Adds where condition using the OR operator between parameters.

order(string $columns, ...$params)

Adds order clause, more calls appends to the end.

limit(int $limit, int $offset = null)

Sets limit clause, more calls rewrite old values.

page(int $page, int $itemsPerPage, $numOfPages = null)

Sets offset using page number, more calls rewrite old values.

group(string $columns, ...$params)

Sets group clause, more calls rewrite old value.

having(string $having, ...$params)

Sets having clause, more calls rewrite old value.

alias(string $tableChain, string $alias)

Aliases table. Example ':book:book_tag.tag', 'tg'

mixed
aggregation(string $function)

Executes aggregation function.

int
count(string $column = null)

Counts number of rows.

mixed
min(string $column)

Returns minimum value from a column.

mixed
max(string $column)

Returns maximum value from a column.

mixed
sum(string $column)

Returns sum of values in a column.

void
execute()

No description

createRow(array $row)

No description

createSelectionInstance(string $table = null)

No description

createGroupedSelectionInstance(string $table, string $column)

No description

query(string $query)

No description

void
emptyResultSet(bool $clearCache = true, bool $deleteRererencedCache = true)

No description

getRefTable($refPath)

Returns Selection parent for caching.

void
loadRefCache()

Loads refCache references

void
linkRefCache()

Link refCache references

bool
accessColumn(string $key, bool $selectColumn = true)

No description

void
removeAccessColumn(string $key)

No description

bool
reloadAllColumns()

No description

void
refreshData()

No description

ActiveRow|int|bool
insert(iterable $data)

Inserts row in a table.

int
update(iterable $data)

Updates all rows in result set.

int
delete()

Deletes all rows in result set.

ActiveRow|null|false
getReferencedTable(ActiveRow $row, string|null $table, string $column = null)

Returns referenced row.

GroupedSelection|null
getReferencingTable(string $table, string $column = null, int $active = null)

Returns referencing rows.

void
rewind()

No description

ActiveRow|false
current()

No description

string|int
key()

No description

void
next()

No description

bool
valid()

No description

void
offsetSet(string $key, IRow $value)

Mimic row.

ActiveRow|null
offsetGet(string $key)

Returns specified row.

bool
offsetExists(string $key)

Tests if row exists.

void
offsetUnset(string $key)

Removes row from result set.

Details

at line 71
__construct(Context $context, IConventions $conventions, string $tableName, IStorage $cacheStorage = null)

Creates filtered table representation.

Parameters

Context $context
IConventions $conventions
string $tableName
IStorage $cacheStorage

at line 85
__destruct()

at line 91
__clone()

at line 100
string getName()

Return Value

string

at line 109
string|array|null getPrimary(bool $throw = true)

Parameters

bool $throw

Return Value

string|array|null

at line 118
string|null getPrimarySequence()

Return Value

string|null

at line 131
Selection setPrimarySequence(string $sequence)

Parameters

string $sequence

Return Value

Selection

at line 138
string getSql()

Return Value

string

at line 147
SqlBuilder getSqlBuilder()

Return Value

SqlBuilder

at line 156
ColumnAccessCache getCache()

Return Value

ColumnAccessCache

at line 165
getGlobalRefCache(string $key)

Parameters

string $key

at line 174
Connection getConnection()

Return Value

Connection

at line 187
ActiveRow|null get(mixed $key)

Returns row specified by primary key.

Parameters

mixed $key primary key

Return Value

ActiveRow|null

at line 198
IRow|null fetch()

Fetches single row object.

Return Value

IRow|null

at line 212
mixed fetchField(string $column = null) deprecated

deprecated

Fetches single field.

Parameters

string $column

Return Value

mixed

at line 230
array fetchPairs(string|int $key = null, string|int $value = null)

Fetches all rows as associative array.

Parameters

string|int $key column name used for an array key or null for numeric index
string|int $value column name used for an array value or null for the whole row

Return Value

array

at line 239
array fetchAll()

Fetches all rows.

Return Value

array

at line 248
array fetchAssoc(string $path)

Fetches all rows and returns associative tree.

Parameters

string $path associative descriptor

Return Value

array

at line 263
Selection select($columns, ...$params)

Adds select clause, more calls appends to the end.

Parameters

$columns
...$params

Return Value

Selection

at line 276
Selection wherePrimary(mixed $key)

Adds condition for primary key.

Parameters

mixed $key

Return Value

Selection

at line 301
Selection where($condition, ...$params)

Adds where condition, more calls appends with AND.

Parameters

$condition
...$params

Return Value

Selection

at line 314
Selection joinWhere(string $tableChain, string $condition, ...$params)

Adds ON condition when joining specified table, more calls appends with AND.

Parameters

string $tableChain
string $condition
...$params

Return Value

Selection

at line 325
protected void condition($condition, array $params, $tableChain = null)

Adds condition, more calls appends with AND.

Parameters

$condition
array $params
$tableChain

Return Value

void

at line 351
Selection whereOr(array $parameters)

Adds where condition using the OR operator between parameters.

More calls appends with AND.

Parameters

array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition']

Return Value

Selection

Exceptions

InvalidArgumentException

at line 383
Selection order(string $columns, ...$params)

Adds order clause, more calls appends to the end.

Parameters

string $columns
...$params

Return Value

Selection

at line 395
Selection limit(int $limit, int $offset = null)

Sets limit clause, more calls rewrite old values.

Parameters

int $limit
int $offset

Return Value

Selection

at line 407
Selection page(int $page, int $itemsPerPage, $numOfPages = null)

Sets offset using page number, more calls rewrite old values.

Parameters

int $page
int $itemsPerPage
$numOfPages

Return Value

Selection

at line 423
Selection group(string $columns, ...$params)

Sets group clause, more calls rewrite old value.

Parameters

string $columns
...$params

Return Value

Selection

at line 435
Selection having(string $having, ...$params)

Sets having clause, more calls rewrite old value.

Parameters

string $having
...$params

Return Value

Selection

at line 447
Selection alias(string $tableChain, string $alias)

Aliases table. Example ':book:book_tag.tag', 'tg'

Parameters

string $tableChain
string $alias

Return Value

Selection

at line 462
mixed aggregation(string $function)

Executes aggregation function.

Parameters

string $function select call in "FUNCTION(column)" format

Return Value

mixed

at line 477
int count(string $column = null)

Counts number of rows.

Parameters

string $column if it is not provided returns count of result rows, otherwise runs new sql counting query

Return Value

int

at line 491
mixed min(string $column)

Returns minimum value from a column.

Parameters

string $column

Return Value

mixed

at line 501
mixed max(string $column)

Returns maximum value from a column.

Parameters

string $column

Return Value

mixed

at line 511
mixed sum(string $column)

Returns sum of values in a column.

Parameters

string $column

Return Value

mixed

at line 520
protected void execute()

Return Value

void

at line 563
protected ActiveRow createRow(array $row)

Parameters

array $row

Return Value

ActiveRow

at line 569
Selection createSelectionInstance(string $table = null)

Parameters

string $table

Return Value

Selection

at line 575
protected GroupedSelection createGroupedSelectionInstance(string $table, string $column)

Parameters

string $table
string $column

Return Value

GroupedSelection

at line 581
protected ResultSet query(string $query)

Parameters

string $query

Return Value

ResultSet

at line 587
protected void emptyResultSet(bool $clearCache = true, bool $deleteRererencedCache = true)

Parameters

bool $clearCache
bool $deleteRererencedCache

Return Value

void

at line 613
protected Selection getRefTable($refPath)

Returns Selection parent for caching.

Parameters

$refPath

Return Value

Selection

at line 622
protected void loadRefCache()

Loads refCache references

Return Value

void

at line 630
protected void linkRefCache()

Link refCache references

Return Value

void

at line 648
bool accessColumn(string $key, bool $selectColumn = true)

Parameters

string $key
bool $selectColumn

Return Value

bool if selection requeried for more columns.

at line 668
void removeAccessColumn(string $key)

Parameters

string $key

Return Value

void

at line 678
bool reloadAllColumns()

Return Value

bool if selection requeried for reload.

at line 701
protected void refreshData()

Return Value

void

at line 740
ActiveRow|int|bool insert(iterable $data)

Inserts row in a table.

Parameters

iterable $data [$column => $value]|\Traversable|Selection for INSERT ... SELECT

Return Value

ActiveRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key

at line 820
int update(iterable $data)

Updates all rows in result set.

Joins in UPDATE are supported only in MySQL

Parameters

iterable $data

Return Value

int number of affected rows

at line 844
int delete()

Deletes all rows in result set.

Return Value

int number of affected rows

at line 857
ActiveRow|null|false getReferencedTable(ActiveRow $row, string|null $table, string $column = null)

Returns referenced row.

Parameters

ActiveRow $row
string|null $table
string $column

Return Value

ActiveRow|null|false null if the row does not exist, false if the relationship does not exist

at line 903
GroupedSelection|null getReferencingTable(string $table, string $column = null, int $active = null)

Returns referencing rows.

Parameters

string $table
string $column
int $active

Return Value

GroupedSelection|null

at line 930
void rewind()

Return Value

void

at line 939
ActiveRow|false current()

Return Value

ActiveRow|false

at line 952
string|int key()

Return Value

string|int row ID

at line 958
void next()

Return Value

void

at line 966
bool valid()

Return Value

bool

at line 980
void offsetSet(string $key, IRow $value)

Mimic row.

Parameters

string $key
IRow $value

Return Value

void

at line 991
ActiveRow|null offsetGet(string $key)

Returns specified row.

Parameters

string $key

Return Value

ActiveRow|null

at line 1002
bool offsetExists(string $key)

Tests if row exists.

Parameters

string $key

Return Value

bool

at line 1013
void offsetUnset(string $key)

Removes row from result set.

Parameters

string $key

Return Value

void