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.

Properties

protected Explorer $explorer
protected Explorer $context
protected Conventions $conventions
protected Cache $cache
protected SqlBuilder $sqlBuilder
protected string $name
protected string|string[]|null $primary
protected string|bool $primarySequence
protected ActiveRow[] $rows
protected ActiveRow[] $data
protected bool $dataRefreshed
protected mixed $globalRefCache
protected mixed $refCache
protected string|null $generalCacheKey
protected string|null $specificCacheKey
protected array $aggregation
protected array|false|null $accessedColumns
protected array|false|null $previousAccessedColumns
protected Selection|null $observeCache
protected array $keys

Methods

__construct(Explorer $explorer, Conventions $conventions, string $tableName, IStorage $cacheStorage = null)

Creates filtered table representation.

__destruct()

No description

__clone()

No description

string
getName()

No description

string|string[]|null
getPrimary(bool $throw = true)

No description

string|null
getPrimarySequence()

No description

setPrimarySequence(string $sequence)

No description

string
getSql()

No description

array|bool
getPreviousAccessedColumns()

Loads cache of previous accessed columns and returns it.

getSqlBuilder()

No description

ActiveRow|null
get(mixed $key)

Returns row specified by primary key.

ActiveRow|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(string|string[] $columns, ...$params)

Adds select clause, more calls appends to the end.

wherePrimary(mixed $key)

Adds condition for primary key.

where(string|array $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(string|string[] $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|null $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, string $groupFunction = null)

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

void
saveCacheState()

No description

getRefTable($refPath)

Returns Selection parent for caching.

void
loadRefCache()

Loads refCache references

string
getGeneralCacheKey()

Returns general cache key independent on query parameters or sql limit Used e.g. for previously accessed columns caching

string
getSpecificCacheKey()

Returns object specific cache key dependent on query parameters Used e.g. for reference memory caching

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

No description

void
removeAccessColumn(string $key)

No description

bool
getDataRefreshed()

Returns if selection requeried for more columns.

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|false|null
getReferencedTable(ActiveRow $row, string|null $table, string $column = null)

Returns referenced row.

GroupedSelection|null
getReferencingTable(string $table, string $column = null, int|string $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, ActiveRow $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 89
__construct(Explorer $explorer, Conventions $conventions, string $tableName, IStorage $cacheStorage = null)

Creates filtered table representation.

Parameters

Explorer $explorer
Conventions $conventions
string $tableName
IStorage $cacheStorage

at line 108
__destruct()

No description

at line 114
__clone()

No description

at line 120
string getName()

No description

Return Value

string

at line 129
string|string[]|null getPrimary(bool $throw = true)

No description

Parameters

bool $throw

Return Value

string|string[]|null

at line 138
string|null getPrimarySequence()

No description

Return Value

string|null

at line 151
Selection setPrimarySequence(string $sequence)

No description

Parameters

string $sequence

Return Value

Selection

at line 158
string getSql()

No description

Return Value

string

at line 169
array|bool getPreviousAccessedColumns()

internal  
 

Loads cache of previous accessed columns and returns it.

Return Value

array|bool

at line 185
SqlBuilder getSqlBuilder()

internal  
 

No description

Return Value

SqlBuilder

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

Returns row specified by primary key.

Parameters

mixed $key

primary key

Return Value

ActiveRow|null

at line 208
ActiveRow|null fetch()

Fetches single row object.

Return Value

ActiveRow|null

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

deprecated

Fetches single field.

Parameters

string $column

Return Value

mixed

at line 242
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 252
array fetchAll()

Fetches all rows.

Return Value

array

at line 262
array fetchAssoc(string $path)

Fetches all rows and returns associative tree.

Parameters

string $path

associative descriptor

Return Value

array

at line 277
Selection select(string|string[] $columns, ...$params)

Adds select clause, more calls appends to the end.

Parameters

string|string[] $columns

for example "column, MD5(column) AS column_md5"

...$params

Return Value

Selection

at line 290
Selection wherePrimary(mixed $key)

Adds condition for primary key.

Parameters

mixed $key

Return Value

Selection

at line 315
Selection where(string|array $condition, ...$params)

Adds where condition, more calls appends with AND.

Parameters

string|array $condition

possibly containing ?

...$params

Return Value

Selection

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

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

Parameters

string $tableChain

table chain or table alias for which you need additional left join condition

string $condition

possibly containing ?

...$params

Return Value

Selection

at line 339
protected void condition(string|string[] $condition, array $params, $tableChain = null)

Adds condition, more calls appends with AND.

Parameters

string|string[] $condition

possibly containing ?

array $params
$tableChain

Return Value

void

at line 365
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 397
Selection order(string $columns, ...$params)

Adds order clause, more calls appends to the end.

Parameters

string $columns

for example 'column1, column2 DESC'

...$params

Return Value

Selection

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

Sets limit clause, more calls rewrite old values.

Parameters

int|null $limit
int $offset

Return Value

Selection

at line 421
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 437
Selection group(string $columns, ...$params)

Sets group clause, more calls rewrite old value.

Parameters

string $columns
...$params

Return Value

Selection

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

Sets having clause, more calls rewrite old value.

Parameters

string $having
...$params

Return Value

Selection

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

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

Parameters

string $tableChain
string $alias

Return Value

Selection

at line 476
mixed aggregation(string $function, string $groupFunction = null)

Executes aggregation function.

Parameters

string $function

select call in "FUNCTION(column)" format

string $groupFunction

Return Value

mixed

at line 497
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 511
mixed min(string $column)

Returns minimum value from a column.

Parameters

string $column

Return Value

mixed

at line 521
mixed max(string $column)

Returns maximum value from a column.

Parameters

string $column

Return Value

mixed

at line 531
mixed sum(string $column)

Returns sum of values in a column.

Parameters

string $column

Return Value

mixed

at line 540
protected void execute()

No description

Return Value

void

at line 583
protected ActiveRow createRow(array $row)

No description

Parameters

array $row

Return Value

ActiveRow

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

No description

Parameters

string $table

Return Value

Selection

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

No description

Parameters

string $table
string $column

Return Value

GroupedSelection

at line 601
protected ResultSet query(string $query)

No description

Parameters

string $query

Return Value

ResultSet

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

No description

Parameters

bool $clearCache
bool $deleteRererencedCache

Return Value

void

at line 629
protected void saveCacheState()

No description

Return Value

void

at line 658
protected Selection getRefTable($refPath)

Returns Selection parent for caching.

Parameters

$refPath

Return Value

Selection

at line 667
protected void loadRefCache()

Loads refCache references

Return Value

void

at line 676
protected string getGeneralCacheKey()

Returns general cache key independent on query parameters or sql limit Used e.g. for previously accessed columns caching

Return Value

string

at line 699
protected string getSpecificCacheKey()

Returns object specific cache key dependent on query parameters Used e.g. for reference memory caching

Return Value

string

at line 714
bool accessColumn(string|null $key, bool $selectColumn = true)

internal  
 

No description

Parameters

string|null $key
bool $selectColumn

Return Value

bool

if selection requeried for more columns.

at line 779
void removeAccessColumn(string $key)

internal  
 

No description

Parameters

string $key

Return Value

void

at line 790
bool getDataRefreshed()

Returns if selection requeried for more columns.

Return Value

bool

at line 804
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 ActiveRow or number of affected rows for Selection or table without primary key

at line 884
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 908
int delete()

Deletes all rows in result set.

Return Value

int

number of affected rows

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

Returns referenced row.

Parameters

ActiveRow $row
string|null $table
string $column

Return Value

ActiveRow|false|null

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

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

Returns referencing rows.

Parameters

string $table
string $column
int|string $active

primary key

Return Value

GroupedSelection|null

at line 994
void rewind()

No description

Return Value

void

at line 1003
ActiveRow|false current()

No description

Return Value

ActiveRow|false

at line 1015
string|int key()

No description

Return Value

string|int

row ID

at line 1022
void next()

No description

Return Value

void

at line 1030
bool valid()

No description

Return Value

bool

at line 1044
void offsetSet(string $key, ActiveRow $value)

Mimic row.

Parameters

string $key
ActiveRow $value

Return Value

void

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

Returns specified row.

Parameters

string $key

Return Value

ActiveRow|null

at line 1066
bool offsetExists(string $key)

Tests if row exists.

Parameters

string $key

Return Value

bool

at line 1077
void offsetUnset(string $key)

Removes row from result set.

Parameters

string $key

Return Value

void

Traits

Strict class for better experience.