Documentation

Db extends Collection
in package

Table of Contents

Constants

SORT_ORDER_ASC  : mixed = 'ASC'
SORT_ORDER_DESC  : mixed = 'DESC'

Properties

$_bindParams  : array<string|int, mixed>
List of binded variables for select
$_cacheConf  : array<string|int, mixed>
Cache configuration array
$_cacheKey  : mixed
$_cacheLifetime  : mixed
$_cacheTags  : mixed
$_conn  : AdapterInterface
DB connection
$_curPage  : int|null
Current page number for items pager
$_data  : array<string|int, mixed>|null
All collection data array Used for getData method
$_fetchStmt  : StatementInterface|null
Database's statement for fetch item one by one
$_filters  : array<string|int, mixed>
Filters configuration
$_flags  : array<string|int, mixed>
Additional collection flags
$_idFieldName  : string
Identifier fild name for collection items
$_isCollectionLoaded  : bool
Loading state flag
$_isFiltersRendered  : bool
Filter rendered flag
$_isOrdersRendered  : bool
Whether orders are rendered
$_itemObjectClass  : string
Item object class name
$_items  : array<string|int, mixed>
Collection items
$_map  : array<string|int, mixed>|null
Fields map for correlation names & real selected fields
$_orders  : array<string|int, mixed>
Order configuration
$_pageSize  : int|false|null
Pager page size
$_select  : Select
Select oblect
$_totalRecords  : int|null
Total items number

Methods

__clone()  : void
Magic clone function
__construct()  : mixed
addBindParam()  : $this
Add variable to bind list
addFieldToFilter()  : $this
Add field filter to collection
addFilter()  : $this
Add collection filter
addFilterToMap()  : $this
Add filter to Map
addItem()  : $this
Adding item to item array
addOrder()  : $this
self::setOrder() alias
clear()  : $this
Clear collection
count()  : int
Retrieve count of collection loaded items
distinct()  : $this
Set select distinct
each()  : void
fetchItem()  : DataObject|bool
Returns a collection item that corresponds to the fetched row and moves the internal data pointer ahead
getAllIds()  : array<string|int, mixed>
Retrieve ids of all tems
getCacheKey()  : string
getCacheLifetime()  : int
getCacheTags()  : array<string|int, mixed>
getColumnValues()  : array<string|int, mixed>
Retrieve field values from all items
getConnection()  : AdapterInterface
Retrieve connection object
getCurPage()  : int
Get current collection page
getData()  : array<string|int, mixed>
Get all data array for collection
getFilter()  : DataObject|array<string|int, mixed>|null
Search for a filter by specified field
getFirstItem()  : DataObject
Retrieve collection first item
getFlag()  : mixed
Retrieve Flag
getIdFieldName()  : string
Id field name getter
getItemByColumnValue()  : DataObject|null
Search first item by field value
getItemById()  : DataObject|null
Retrieve item by id
getItems()  : array<string|int, mixed>
Retrieve collection items
getItemsByColumnValue()  : array<string|int, mixed>
Search all items by field value
getIterator()  : mixed
Implementation of IteratorAggregate::getIterator()
getLastItem()  : DataObject
Retrieve collection last item
getLastPageNumber()  : int
Retrieve collection last page number
getNewEmptyItem()  : DataObject
Retrieve collection empty item
getPageSize()  : int
Retrieve collection page size
getSelect()  : Select
Get Select instance
getSelectCountSql()  : Select
Get SQL for get record count
getSelectSql()  : string|Select
Get sql select string or object
getSize()  : int
Get collection size
hasFlag()  : bool
Has Flag
initCache()  : $this
Initialize collection cache
isLoaded()  : bool
Retrieve collection loading status
load()  : $this
Load data
loadData()  : $this
Load data
printLogQuery()  : $this
Print and/or log query
removeItemByKey()  : $this
Remove item from collection by item key
resetData()  : $this
Reset loaded for collection data array
setCacheKey()  : $this
setCacheTags()  : $this
setConnection()  : $this
Set database connection adapter
setCurPage()  : $this
Set current page
setDataToAll()  : $this
Setting data for all collection items
setFlag()  : $this
Set Flag
setItemObjectClass()  : $this
Set collection item class name
setOrder()  : $this
Add select order
setPageSize()  : $this
Set collection page size
toArray()  : array<string|int, mixed>
Convert collection to array
toOptionArray()  : array<string|int, mixed>
toOptionHash()  : array<string|int, mixed>
toXml()  : string
Convert collection to XML
unshiftOrder()  : $this
Add select order to the beginning
walk()  : array<string|int, mixed>
Walk through the collection and run model method or external callback with optional arguments
_addItem()  : $this
Add item that has no id to collection
_afterLoad()  : $this
_afterLoadData()  : $this
Process loaded collection data
_beforeLoad()  : $this
Before load action
_canUseCache()  : Mage_Core_Model_Cache|false
Check if cache can be used for collection data
_fetchAll()  : array<string|int, mixed>
Fetch collection data
_getCacheTags()  : array<string|int, mixed>
Get cache tags list
_getConditionFieldName()  : string
_getConditionSql()  : string
Build SQL statement for condition
_getItemId()  : mixed
Get collection item identifier
_getMappedField()  : string
Try to get mapped field name for filter to collection
_getMapper()  : array<string|int, mixed>|bool|null
Retrieve mapper data
_getSelectCacheId()  : string
Get cache identifier base on select
_loadCache()  : string|false
Load cached data for select
_renderFilters()  : $this
Render sql select conditions
_renderFiltersBefore()  : mixed
Hook for operations before rendering filters
_renderLimit()  : $this
Render sql select limit
_renderOrders()  : $this
Render sql select orders
_reset()  : $this
Reset collection
_saveCache()  : $this
Save collection data to cache
_setIdFieldName()  : $this
Specify collection objects id field name
_setIsLoaded()  : $this
Set collection loading status flag
_toOptionArray()  : array<string|int, mixed>
Convert items array to array for select options
_toOptionHash()  : array<string|int, mixed>
Convert items array to hash for select options
_toOptionHashOptimized()  : array<string|int, mixed>
Convert items array to hash for select options unsing fetchItem method
_translateCondition()  : string
Build sql where condition part
getCache()  : Mage_Core_Model_Cache|null
_setOrder()  : $this
Add ORDERBY to the end or to the beginning

Constants

SORT_ORDER_ASC

public mixed SORT_ORDER_ASC = 'ASC'

SORT_ORDER_DESC

public mixed SORT_ORDER_DESC = 'DESC'

Properties

$_bindParams

List of binded variables for select

protected array<string|int, mixed> $_bindParams = []

$_cacheConf

Cache configuration array

protected array<string|int, mixed> $_cacheConf = null

$_cacheLifetime

protected mixed $_cacheLifetime = 86400

$_curPage

Current page number for items pager

protected int|null $_curPage = 1

$_data

All collection data array Used for getData method

protected array<string|int, mixed>|null $_data = null

$_fetchStmt

Database's statement for fetch item one by one

protected StatementInterface|null $_fetchStmt = null

$_filters

Filters configuration

protected array<string|int, mixed> $_filters = []

$_flags

Additional collection flags

protected array<string|int, mixed> $_flags = []

$_idFieldName

Identifier fild name for collection items

protected string $_idFieldName

Can be used by collections with items without defined

$_isCollectionLoaded

Loading state flag

protected bool $_isCollectionLoaded

$_isFiltersRendered

Filter rendered flag

protected bool $_isFiltersRendered = false

$_isOrdersRendered

Whether orders are rendered

protected bool $_isOrdersRendered = false

$_itemObjectClass

Item object class name

protected string $_itemObjectClass = \Maho\DataObject::class

$_items

Collection items

protected array<string|int, mixed> $_items = []

$_map

Fields map for correlation names & real selected fields

protected array<string|int, mixed>|null $_map = null

$_orders

Order configuration

protected array<string|int, mixed> $_orders = []

$_pageSize

Pager page size

protected int|false|null $_pageSize = false

if page size is false, then we works with all items

$_select

Select oblect

protected Select $_select

$_totalRecords

Total items number

protected int|null $_totalRecords

Methods

__clone()

Magic clone function

public __clone() : void

Clone also Select

__construct()

public __construct([mixed $conn = null ]) : mixed
Parameters
$conn : mixed = null

addBindParam()

Add variable to bind list

public addBindParam(string $name, mixed $value) : $this
Parameters
$name : string
$value : mixed
Return values
$this

addFieldToFilter()

Add field filter to collection

public addFieldToFilter(string|array<string|int, mixed> $field[, int|string|array<string|int, mixed>|null $condition = null ]) : $this
Parameters
$field : string|array<string|int, mixed>
$condition : int|string|array<string|int, mixed>|null = null
Tags
see
self::_getConditionSql

for $condition

Return values
$this

addFilter()

Add collection filter

public addFilter(string $field, string|array<string|int, mixed> $value[, string $type = 'and' ]) : $this
Parameters
$field : string
$value : string|array<string|int, mixed>
$type : string = 'and'

and|or|string

Return values
$this

addFilterToMap()

Add filter to Map

public addFilterToMap(string $filter, string $alias[, string $group = 'fields' ]) : $this
Parameters
$filter : string
$alias : string
$group : string = 'fields'

default 'fields'

Return values
$this

addOrder()

self::setOrder() alias

public addOrder(string $field[, string $direction = self::SORT_ORDER_DESC ]) : $this
Parameters
$field : string
$direction : string = self::SORT_ORDER_DESC
Return values
$this

clear()

Clear collection

public clear() : $this
Return values
$this

count()

Retrieve count of collection loaded items

public count() : int
Attributes
#[Override]
#[ReturnTypeWillChange]
Return values
int

distinct()

Set select distinct

public distinct(bool $flag) : $this
Parameters
$flag : bool
Attributes
#[Override]
Return values
$this

each()

public each(callable $obj_method[, array<string|int, mixed> $args = [] ]) : void
Parameters
$obj_method : callable
$args : array<string|int, mixed> = []

fetchItem()

Returns a collection item that corresponds to the fetched row and moves the internal data pointer ahead

public fetchItem() : DataObject|bool
Return values
DataObject|bool

getAllIds()

Retrieve ids of all tems

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

getCacheKey()

public getCacheKey() : string
Return values
string

getCacheLifetime()

public getCacheLifetime() : int
Return values
int

getCacheTags()

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

getColumnValues()

Retrieve field values from all items

public getColumnValues(string $colName) : array<string|int, mixed>
Parameters
$colName : string
Return values
array<string|int, mixed>

getCurPage()

Get current collection page

public getCurPage([int $displacement = 0 ]) : int
Parameters
$displacement : int = 0
Return values
int

getData()

Get all data array for collection

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

getFilter()

Search for a filter by specified field

public getFilter(string|array<string|int, mixed> $field) : DataObject|array<string|int, mixed>|null

Multiple filters can be matched if an array is specified:

  • 'foo' -- get the first filter with field name 'foo'
  • array('foo') -- get all filters with field name 'foo'
  • array('foo', 'bar') -- get all filters with field name 'foo' or 'bar'
  • array() -- get all filters
Parameters
$field : string|array<string|int, mixed>
Return values
DataObject|array<string|int, mixed>|null

getFlag()

Retrieve Flag

public getFlag(string $flag) : mixed
Parameters
$flag : string

getIdFieldName()

Id field name getter

public getIdFieldName() : string
Return values
string

getItemByColumnValue()

Search first item by field value

public getItemByColumnValue(string $column, mixed $value) : DataObject|null
Parameters
$column : string
$value : mixed
Return values
DataObject|null

getItems()

Retrieve collection items

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

getItemsByColumnValue()

Search all items by field value

public getItemsByColumnValue(string $column, mixed $value) : array<string|int, mixed>
Parameters
$column : string
$value : mixed
Return values
array<string|int, mixed>

getIterator()

Implementation of IteratorAggregate::getIterator()

public getIterator() : mixed
Attributes
#[Override]
#[ReturnTypeWillChange]

getLastPageNumber()

Retrieve collection last page number

public getLastPageNumber() : int
Return values
int

getPageSize()

Retrieve collection page size

public getPageSize() : int
Return values
int

getSelect()

Get Select instance

public getSelect() : Select
Return values
Select

getSelectCountSql()

Get SQL for get record count

public getSelectCountSql() : Select
Return values
Select

getSelectSql()

Get sql select string or object

public getSelectSql([bool $stringMode = false ]) : string|Select
Parameters
$stringMode : bool = false
Return values
string|Select

getSize()

Get collection size

public getSize() : int
Attributes
#[Override]
Return values
int

hasFlag()

Has Flag

public hasFlag(string $flag) : bool
Parameters
$flag : string
Return values
bool

initCache()

Initialize collection cache

public initCache(mixed $object, string $idPrefix, array<string|int, mixed> $tags) : $this
Parameters
$object : mixed
$idPrefix : string
$tags : array<string|int, mixed>
Return values
$this

isLoaded()

Retrieve collection loading status

public isLoaded() : bool
Return values
bool

load()

Load data

public load([bool $printQuery = false ][, bool $logQuery = false ]) : $this
Parameters
$printQuery : bool = false
$logQuery : bool = false
Attributes
#[Override]
Return values
$this

loadData()

Load data

public loadData([bool $printQuery = false ][, bool $logQuery = false ]) : $this
Parameters
$printQuery : bool = false
$logQuery : bool = false
Attributes
#[Override]
Return values
$this

printLogQuery()

Print and/or log query

public printLogQuery([bool $printQuery = false ][, bool $logQuery = false ][, string $sql = null ]) : $this
Parameters
$printQuery : bool = false
$logQuery : bool = false
$sql : string = null
Return values
$this

removeItemByKey()

Remove item from collection by item key

public removeItemByKey(mixed $key) : $this
Parameters
$key : mixed
Return values
$this

resetData()

Reset loaded for collection data array

public resetData() : $this
Return values
$this

setCacheKey()

public setCacheKey(string $key) : $this
Parameters
$key : string
Return values
$this

setCacheTags()

public setCacheTags(array<string|int, mixed> $tags) : $this
Parameters
$tags : array<string|int, mixed>
Return values
$this

setCurPage()

Set current page

public setCurPage(int|null $page) : $this
Parameters
$page : int|null
Return values
$this

setDataToAll()

Setting data for all collection items

public setDataToAll(mixed $key[, mixed $value = null ]) : $this
Parameters
$key : mixed
$value : mixed = null
Return values
$this

setFlag()

Set Flag

public setFlag(string $flag[, mixed $value = null ]) : $this
Parameters
$flag : string
$value : mixed = null
Return values
$this

setItemObjectClass()

Set collection item class name

public setItemObjectClass(string $className) : $this
Parameters
$className : string
Return values
$this

setOrder()

Add select order

public setOrder(string $field[, string $direction = self::SORT_ORDER_DESC ]) : $this
Parameters
$field : string
$direction : string = self::SORT_ORDER_DESC
Attributes
#[Override]
Return values
$this

setPageSize()

Set collection page size

public setPageSize(int|null $size) : $this
Parameters
$size : int|null
Return values
$this

toArray()

Convert collection to array

public toArray([array<string|int, mixed> $arrRequiredFields = [] ]) : array<string|int, mixed>
Parameters
$arrRequiredFields : array<string|int, mixed> = []
Return values
array<string|int, mixed>

toOptionArray()

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

toOptionHash()

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

toXml()

Convert collection to XML

public toXml() : string
Return values
string

unshiftOrder()

Add select order to the beginning

public unshiftOrder(string $field[, string $direction = self::SORT_ORDER_DESC ]) : $this
Parameters
$field : string
$direction : string = self::SORT_ORDER_DESC
Return values
$this

walk()

Walk through the collection and run model method or external callback with optional arguments

public walk(string|callable $callback[, array<string|int, mixed> $args = [] ]) : array<string|int, mixed>

Returns array with results of callback for each item

Parameters
$callback : string|callable
$args : array<string|int, mixed> = []
Return values
array<string|int, mixed>

_afterLoad()

protected _afterLoad() : $this
Return values
$this

_afterLoadData()

Process loaded collection data

protected _afterLoadData() : $this
Return values
$this

_beforeLoad()

Before load action

protected _beforeLoad() : $this
Return values
$this

_fetchAll()

Fetch collection data

protected _fetchAll(Select|string $select) : array<string|int, mixed>
Parameters
$select : Select|string
Return values
array<string|int, mixed>

_getCacheTags()

Get cache tags list

protected _getCacheTags() : array<string|int, mixed>
Return values
array<string|int, mixed>

_getConditionFieldName()

protected _getConditionFieldName(string $fieldName) : string
Parameters
$fieldName : string
Return values
string

_getConditionSql()

Build SQL statement for condition

protected _getConditionSql(string $fieldName, int|string|array<string|int, mixed> $condition) : string

If $condition integer or string - exact value will be filtered ('eq' condition)

If $condition is array - one of the following structures is expected:

  • array("from" => $fromValue, "to" => $toValue)
  • array("eq" => $equalValue)
  • array("neq" => $notEqualValue)
  • array("like" => $likeValue)
  • array("in" => array($inValues))
  • array("nin" => array($notInValues))
  • array("notnull" => $valueIsNotNull)
  • array("null" => $valueIsNull)
  • array("moreq" => $moreOrEqualValue)
  • array("gt" => $greaterValue)
  • array("lt" => $lessValue)
  • array("gteq" => $greaterOrEqualValue)
  • array("lteq" => $lessOrEqualValue)
  • array("finset" => $valueInSet)
  • array("regexp" => $regularExpression)
  • array("seq" => $stringValue)
  • array("sneq" => $stringValue)

If non matched - sequential array is expected and OR conditions will be built using above mentioned structure

Parameters
$fieldName : string

Field name must be already escaped with AdapterInterface::quoteIdentifier()

$condition : int|string|array<string|int, mixed>
Return values
string

_getItemId()

Get collection item identifier

protected _getItemId(DataObject $item) : mixed
Parameters
$item : DataObject
Attributes
#[Override]

_getMappedField()

Try to get mapped field name for filter to collection

protected _getMappedField(string $field) : string
Parameters
$field : string
Return values
string

_getMapper()

Retrieve mapper data

protected _getMapper() : array<string|int, mixed>|bool|null
Return values
array<string|int, mixed>|bool|null

_getSelectCacheId()

Get cache identifier base on select

protected _getSelectCacheId(Select|string $select) : string
Parameters
$select : Select|string
Return values
string

_loadCache()

Load cached data for select

protected _loadCache(Select $select) : string|false
Parameters
$select : Select
Return values
string|false

_renderFilters()

Render sql select conditions

protected _renderFilters() : $this
Attributes
#[Override]
Return values
$this

_renderFiltersBefore()

Hook for operations before rendering filters

protected _renderFiltersBefore() : mixed

_renderLimit()

Render sql select limit

protected _renderLimit() : $this
Attributes
#[Override]
Return values
$this

_renderOrders()

Render sql select orders

protected _renderOrders() : $this
Attributes
#[Override]
Return values
$this

_reset()

Reset collection

protected _reset() : $this
Return values
$this

_saveCache()

Save collection data to cache

protected _saveCache(array<string|int, mixed> $data, Select $select) : $this
Parameters
$data : array<string|int, mixed>
$select : Select
Return values
$this

_setIdFieldName()

Specify collection objects id field name

protected _setIdFieldName(string $fieldName) : $this
Parameters
$fieldName : string
Return values
$this

_setIsLoaded()

Set collection loading status flag

protected _setIsLoaded([bool $flag = true ]) : $this
Parameters
$flag : bool = true
Return values
$this

_toOptionArray()

Convert items array to array for select options

protected _toOptionArray([string $valueField = 'id' ][, string $labelField = 'name' ][, array<string|int, mixed> $additional = [] ]) : array<string|int, mixed>

return items array array( $index => array( 'value' => mixed 'label' => mixed ) )

Parameters
$valueField : string = 'id'
$labelField : string = 'name'
$additional : array<string|int, mixed> = []
Return values
array<string|int, mixed>

_toOptionHash()

Convert items array to hash for select options

protected _toOptionHash([string $valueField = 'id' ][, string $labelField = 'name' ]) : array<string|int, mixed>

return items hash array($value => $label)

Parameters
$valueField : string = 'id'
$labelField : string = 'name'
Return values
array<string|int, mixed>

_toOptionHashOptimized()

Convert items array to hash for select options unsing fetchItem method

protected _toOptionHashOptimized([string $valueField = 'id' ][, string $labelField = 'name' ]) : array<string|int, mixed>

The difference between _toOptionHash() and this one is that this method fetch items one by one and does not load all collection items at once return items hash array($value => $label)

Parameters
$valueField : string = 'id'
$labelField : string = 'name'
Tags
see
fetchItem()
Return values
array<string|int, mixed>

_translateCondition()

Build sql where condition part

protected _translateCondition(string|array<string|int, mixed> $field, int|string|array<string|int, mixed> $condition) : string
Parameters
$field : string|array<string|int, mixed>
$condition : int|string|array<string|int, mixed>
Return values
string

_setOrder()

Add ORDERBY to the end or to the beginning

private _setOrder(string $field, string $direction[, bool $unshift = false ]) : $this
Parameters
$field : string
$direction : string
$unshift : bool = false
Return values
$this
On this page

Search results