Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
AbstractCollection Class Reference
Inheritance diagram for AbstractCollection:
AbstractDb SourceProviderInterface Collection ArrayInterface CollectionDataSourceInterface OptionSourceInterface ArgumentInterface Collection Critical Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection ProductCollection Collection AbstractCollection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Uut SearchResult Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection LowQuantityCollection TableCollection MessageCollection MessageStatusCollection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection AbstractCollection Collection Collection Collection Collection Collection Collection Collection AbstractCollection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Scoped Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection UrlRewriteCollection Collection Collection Collection Collection Collection Collection Collection Collection Collection Collection

Public Member Functions

 __construct (\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
 
 getMainTable ()
 
 setMainTable ($table)
 
 getSelect ()
 
 addFieldToSelect ($field, $alias=null)
 
 addExpressionFieldToSelect ($alias, $expression, $fields)
 
 removeFieldFromSelect ($field, $isAlias=false)
 
 removeAllFieldsFromSelect ()
 
 setModel ($model)
 
 getModelName ()
 
 setResourceModel ($model)
 
 getResourceModelName ()
 
 getResource ()
 
 getTable ($table)
 
 getAllIds ()
 
 join ($table, $cond, $cols=' *')
 
 setResetItemsDataChanged ($flag)
 
 resetItemsDataChanged ()
 
 save ()
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from AbstractDb
 __construct (EntityFactoryInterface $entityFactory, Logger $logger, FetchStrategyInterface $fetchStrategy, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
 
 getResource ()
 
 addBindParam ($name, $value)
 
 getIdFieldName ()
 
 setConnection (\Magento\Framework\DB\Adapter\AdapterInterface $conn)
 
 getSelect ()
 
 getConnection ()
 
 getSize ()
 
 getSelectCountSql ()
 
 getSelectSql ($stringMode=false)
 
 setOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 addOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 unshiftOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 addFieldToFilter ($field, $condition=null)
 
 distinct ($flag)
 
 load ($printQuery=false, $logQuery=false)
 
 loadWithFilter ($printQuery=false, $logQuery=false)
 
 fetchItem ()
 
 getData ()
 
 resetData ()
 
 loadData ($printQuery=false, $logQuery=false)
 
 printLogQuery ($printQuery=false, $logQuery=false, $sql=null)
 
 addFilterToMap ($filter, $alias, $group='fields')
 
 __clone ()
 
 joinExtensionAttribute (JoinDataInterface $join, JoinProcessorInterface $extensionAttributesJoinProcessor)
 
 getItemObjectClass ()
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from Collection
 __construct (EntityFactoryInterface $entityFactory)
 
 addFilter ($field, $value, $type='and')
 
 addFieldToFilter ($field, $condition)
 
 getFilter ($field)
 
 isLoaded ()
 
 getCurPage ($displacement=0)
 
 getLastPageNumber ()
 
 getPageSize ()
 
 getSize ()
 
 getFirstItem ()
 
 getLastItem ()
 
 getItems ()
 
 getColumnValues ($colName)
 
 getItemsByColumnValue ($column, $value)
 
 getItemByColumnValue ($column, $value)
 
 addItem (\Magento\Framework\DataObject $item)
 
 getAllIds ()
 
 removeItemByKey ($key)
 
 removeAllItems ()
 
 clear ()
 
 walk ($callback, array $args=[])
 
 each ($objMethod, $args=[])
 
 setDataToAll ($key, $value=null)
 
 setCurPage ($page)
 
 setPageSize ($size)
 
 setOrder ($field, $direction=self::SORT_ORDER_DESC)
 
 setItemObjectClass ($className)
 
 getNewEmptyItem ()
 
 distinct ($flag)
 
 loadData ($printQuery=false, $logQuery=false)
 
 load ($printQuery=false, $logQuery=false)
 
 loadWithFilter ($printQuery=false, $logQuery=false)
 
 toXml ()
 
 toArray ($arrRequiredFields=[])
 
 toOptionArray ()
 
 toOptionHash ()
 
 getItemById ($idValue)
 
 getIterator ()
 
 count ()
 
 getFlag ($flag)
 
 setFlag ($flag, $value=null)
 
 hasFlag ($flag)
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from SourceProviderInterface
 getIdFieldName ()
 
 addFieldToFilter ($attribute, $condition=null)
 

Protected Member Functions

 _construct ()
 
 _initSelect ()
 
 _initSelectFields ()
 
 _getInitialFieldsToSelect ()
 
 _initInitialFieldsToSelect ()
 
 _init ($model, $resourceModel)
 
 _beforeLoad ()
 
 _afterLoad ()
 
- Protected Member Functions inherited from AbstractDb
 _setIdFieldName ($fieldName)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
 _renderFilters ()
 
 _renderFiltersBefore ()
 
 _translateCondition ($field, $condition)
 
 _getMappedField ($field)
 
 _getMapper ()
 
 _getConditionSql ($fieldName, $condition)
 
 _getConditionFieldName ($fieldName)
 
 _renderOrders ()
 
 _renderLimit ()
 
 _beforeLoad ()
 
 beforeAddLoadedItem (\Magento\Framework\DataObject $item)
 
 _toOptionArray ($valueField=null, $labelField='name', $additional=[])
 
 _toOptionHash ($valueField=null, $labelField='name')
 
 _afterLoadData ()
 
 _afterLoad ()
 
 _logQuery ($sql)
 
 _reset ()
 
 _fetchAll (Select $select)
 
 _initSelect ()
 
- Protected Member Functions inherited from Collection
 _setIsLoaded ($flag=true)
 
 _addItem ($item)
 
 _getItemId (\Magento\Framework\DataObject $item)
 
 _renderFilters ()
 
 _renderOrders ()
 
 _renderLimit ()
 
 _toOptionArray ($valueField='id', $labelField='name', $additional=[])
 
 _toOptionHash ($valueField='id', $labelField='name')
 

Protected Attributes

 $_model
 
 $_resourceModel
 
 $_resource
 
 $_fieldsToSelect = null
 
 $_initialFieldsToSelect = null
 
 $_fieldsToSelectChanged = false
 
 $_joinedTables = []
 
 $_mainTable = null
 
 $_resetItemsDataChanged = false
 
 $_eventPrefix = ''
 
 $_eventObject = ''
 
 $_eventManager = null
 
- Protected Attributes inherited from AbstractDb
 $_conn
 
 $_select
 
 $_idFieldName
 
 $_bindParams = []
 
 $_data = null
 
 $_map = null
 
 $_fetchStmt = null
 
 $_isOrdersRendered = false
 
 $_logger
 
 $extensionAttributesJoinProcessor
 
- Protected Attributes inherited from Collection
 $_items = []
 
 $_itemObjectClass = \Magento\Framework\DataObject::class
 
 $_orders = []
 
 $_filters = []
 
 $_isFiltersRendered = false
 
 $_curPage = 1
 
 $_pageSize = false
 
 $_totalRecords
 
 $_isCollectionLoaded
 
 $_flags = []
 
 $_entityFactory
 

Additional Inherited Members

- Data Fields inherited from Collection
const SORT_ORDER_ASC = 'ASC'
 
const SORT_ORDER_DESC = 'DESC'
 

Detailed Description

Abstract Resource Collection

@api @SuppressWarnings(PHPMD.NumberOfChildren)

Since
100.0.2

Definition at line 19 of file AbstractCollection.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Framework\Data\Collection\EntityFactoryInterface$entityFactory
\Psr\Log\LoggerInterface$logger
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface$fetchStrategy
\Magento\Framework\Event\ManagerInterface$eventManager
\Magento\Framework\DB\Adapter\AdapterInterface$connection
\Magento\Framework\Model\ResourceModel\Db\AbstractDb$resource

Definition at line 113 of file AbstractCollection.php.

120  {
121  $this->_eventManager = $eventManager;
122  parent::__construct($entityFactory, $logger, $fetchStrategy, $connection);
123  $this->_construct();
124  $this->_resource = $resource;
125  $this->setConnection($this->getResource()->getConnection());
126  $this->_initSelect();
127  }
$resource
Definition: bulk.php:12
$logger
setConnection(\Magento\Framework\DB\Adapter\AdapterInterface $conn)
Definition: AbstractDb.php:184
$connection
Definition: bulk.php:13

Member Function Documentation

◆ __sleep()

__sleep ( )
Since
100.0.11

Definition at line 603 of file AbstractCollection.php.

604  {
605  return array_diff(
606  parent::__sleep(),
607  ['_resource', '_eventManager']
608  );
609  }

◆ __wakeup()

__wakeup ( )
Since
100.0.11

Definition at line 615 of file AbstractCollection.php.

616  {
619  $this->_eventManager = $objectManager->get(\Magento\Framework\Event\ManagerInterface::class);
620  }
$objectManager
Definition: bootstrap.php:17

◆ _afterLoad()

_afterLoad ( )
protected

Redeclare after load method for specifying collection items original data

Returns
$this

Definition at line 570 of file AbstractCollection.php.

571  {
572  parent::_afterLoad();
573  foreach ($this->_items as $item) {
574  $item->setOrigData();
575  if ($this->_resetItemsDataChanged && ($item instanceof \Magento\Framework\Model\AbstractModel)) {
576  $item->setDataChanges(false);
577  }
578  }
579  $this->_eventManager->dispatch('core_collection_abstract_load_after', ['collection' => $this]);
580  if ($this->_eventPrefix && $this->_eventObject) {
581  $this->_eventManager->dispatch($this->_eventPrefix . '_load_after', [$this->_eventObject => $this]);
582  }
583  return $this;
584  }

◆ _beforeLoad()

_beforeLoad ( )
protected

Redeclare before load method for adding event

Returns
$this

Definition at line 529 of file AbstractCollection.php.

530  {
531  parent::_beforeLoad();
532  $this->_eventManager->dispatch('core_collection_abstract_load_before', ['collection' => $this]);
533  if ($this->_eventPrefix && $this->_eventObject) {
534  $this->_eventManager->dispatch($this->_eventPrefix . '_load_before', [$this->_eventObject => $this]);
535  }
536  return $this;
537  }

◆ _construct()

_construct ( )
protected

Initialization here

Returns
void

Definition at line 134 of file AbstractCollection.php.

135  {
136  }

◆ _getInitialFieldsToSelect()

_getInitialFieldsToSelect ( )
protected

Retrieve initial fields to select like id field

Returns
array

Definition at line 264 of file AbstractCollection.php.

265  {
266  if ($this->_initialFieldsToSelect === null) {
267  $this->_initialFieldsToSelect = [];
269  }
270 
272  }

◆ _init()

_init (   $model,
  $resourceModel 
)
protected

Standard resource collection initialization

Parameters
string$model
string$resourceModel
Returns
$this

Definition at line 401 of file AbstractCollection.php.

◆ _initInitialFieldsToSelect()

_initInitialFieldsToSelect ( )
protected

Initialize initial fields to select like id field

Returns
$this

Definition at line 279 of file AbstractCollection.php.

280  {
281  $idFieldName = $this->getResource()->getIdFieldName();
282  if ($idFieldName) {
283  $this->_initialFieldsToSelect[] = $idFieldName;
284  }
285  return $this;
286  }

◆ _initSelect()

_initSelect ( )
protected

◆ _initSelectFields()

_initSelectFields ( )
protected

Init fields for select

Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 203 of file AbstractCollection.php.

204  {
205  $columns = $this->_select->getPart(\Magento\Framework\DB\Select::COLUMNS);
206  $columnsToSelect = [];
207  foreach ($columns as $columnEntry) {
208  list($correlationName, $column, $alias) = $columnEntry;
209  if ($correlationName !== 'main_table') {
210  // Add joined fields to select
211  if ($column instanceof \Zend_Db_Expr) {
212  $column = $column->__toString();
213  }
214  $key = $alias !== null ? $alias : $column;
215  $columnsToSelect[$key] = $columnEntry;
216  }
217  }
218 
219  $columns = $columnsToSelect;
220 
221  $columnsToSelect = array_keys($columnsToSelect);
222 
223  if ($this->_fieldsToSelect !== null) {
224  $insertIndex = 0;
225  foreach ($this->_fieldsToSelect as $alias => $field) {
226  if (!is_string($alias)) {
227  $alias = null;
228  }
229 
230  if ($field instanceof \Zend_Db_Expr) {
231  $column = $field->__toString();
232  } else {
233  $column = $field;
234  }
235 
236  if ($alias !== null &&
237  in_array($alias, $columnsToSelect) ||
238  // If field already joined from another table
239  $alias === null &&
240  isset($alias, $columnsToSelect)
241  ) {
242  continue;
243  }
244 
245  $columnEntry = ['main_table', $field, $alias];
246  array_splice($columns, $insertIndex, 0, [$columnEntry]);
247  // Insert column
248  $insertIndex++;
249  }
250  } else {
251  array_unshift($columns, ['main_table', '*', null]);
252  }
253 
254  $this->_select->setPart(\Magento\Framework\DB\Select::COLUMNS, $columns);
255 
256  return $this;
257  }
$columns
Definition: default.phtml:15
if(!trim($html)) $alias
Definition: details.phtml:20

◆ addExpressionFieldToSelect()

addExpressionFieldToSelect (   $alias,
  $expression,
  $fields 
)

Add attribute expression (SUM, COUNT, etc) Example: ('sub_total', 'SUM({{attribute}})', 'revenue') Example: ('sub_total', 'SUM({{revenue}})', 'revenue') For some functions like SUM use groupByAttribute.

Parameters
string$alias
string$expression
array | string$fields
Returns
$this

Definition at line 338 of file AbstractCollection.php.

339  {
340  // validate alias
341  if (!is_array($fields)) {
342  $fields = [$fields => $fields];
343  }
344 
345  $fullExpression = $expression;
346  foreach ($fields as $fieldKey => $fieldItem) {
347  $fullExpression = str_replace('{{' . $fieldKey . '}}', $fieldItem, $fullExpression);
348  }
349 
350  $this->getSelect()->columns([$alias => $fullExpression]);
351 
352  return $this;
353  }
$fields
Definition: details.phtml:14
if(!trim($html)) $alias
Definition: details.phtml:20

◆ addFieldToSelect()

addFieldToSelect (   $field,
  $alias = null 
)

Add field to select

Parameters
string | array$field
string | null$alias
Returns
$this

Implements SourceProviderInterface.

Definition at line 295 of file AbstractCollection.php.

296  {
297  if ($field === '*') {
298  // If we will select all fields
299  $this->_fieldsToSelect = null;
300  $this->_fieldsToSelectChanged = true;
301  return $this;
302  }
303 
304  if (is_array($field)) {
305  if ($this->_fieldsToSelect === null) {
306  $this->_fieldsToSelect = $this->_getInitialFieldsToSelect();
307  }
308 
309  foreach ($field as $key => $value) {
310  $this->addFieldToSelect($value, is_string($key) ? $key : null);
311  }
312 
313  $this->_fieldsToSelectChanged = true;
314  return $this;
315  }
316 
317  if ($alias === null) {
318  $this->_fieldsToSelect[] = $field;
319  } else {
320  $this->_fieldsToSelect[$alias] = $field;
321  }
322 
323  $this->_fieldsToSelectChanged = true;
324  return $this;
325  }
$value
Definition: gender.phtml:16
if(!trim($html)) $alias
Definition: details.phtml:20

◆ getAllIds()

getAllIds ( )

Retrieve all ids for collection

Returns
array

Definition at line 485 of file AbstractCollection.php.

486  {
487  $idsSelect = clone $this->getSelect();
488  $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
489  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
490  $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
491  $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
492 
493  $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
494  return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
495  }

◆ getMainTable()

getMainTable ( )

◆ getModelName()

getModelName ( )

Get model instance

Returns
string

Definition at line 428 of file AbstractCollection.php.

◆ getResource()

getResource ( )

Get resource instance

Returns
\Magento\Framework\Model\ResourceModel\Db\AbstractDb

Definition at line 459 of file AbstractCollection.php.

◆ getResourceModelName()

getResourceModelName ( )

Retrieve resource model name

Returns
string

Definition at line 449 of file AbstractCollection.php.

◆ getSelect()

getSelect ( )

Get \Magento\Framework\DB\Select instance and applies fields to select if needed

Returns
\Magento\Framework\DB\Select

Implements SourceProviderInterface.

Definition at line 187 of file AbstractCollection.php.

188  {
189  if ($this->_select && $this->_fieldsToSelectChanged) {
190  $this->_fieldsToSelectChanged = false;
191  $this->_initSelectFields();
192  }
193  return parent::getSelect();
194  }

◆ getTable()

getTable (   $table)

Retrieve table name

Parameters
string$table
Returns
string

Definition at line 475 of file AbstractCollection.php.

476  {
477  return $this->getResource()->getTable($table);
478  }
$table
Definition: trigger.php:14

◆ join()

join (   $table,
  $cond,
  $cols = '*' 
)

Join table to collection select

Parameters
string | array$table
string$cond
string | array$cols
Returns
$this

Definition at line 505 of file AbstractCollection.php.

506  {
507  if (is_array($table)) {
508  foreach ($table as $k => $v) {
509  $alias = $k;
510  $table = $v;
511  break;
512  }
513  } else {
514  $alias = $table;
515  }
516 
517  if (!isset($this->_joinedTables[$alias])) {
518  $this->getSelect()->join([$alias => $this->getTable($table)], $cond, $cols);
519  $this->_joinedTables[$alias] = true;
520  }
521  return $this;
522  }
if(!trim($html)) $alias
Definition: details.phtml:20
$table
Definition: trigger.php:14

◆ removeAllFieldsFromSelect()

removeAllFieldsFromSelect ( )

Removes all fields from select

Returns
$this

Definition at line 387 of file AbstractCollection.php.

388  {
389  $this->_fieldsToSelect = $this->_getInitialFieldsToSelect();
390  $this->_fieldsToSelectChanged = true;
391  return $this;
392  }

◆ removeFieldFromSelect()

removeFieldFromSelect (   $field,
  $isAlias = false 
)

Removes field from select

Parameters
string | null$field
bool$isAliasAlias identifier
Returns
$this

Definition at line 362 of file AbstractCollection.php.

363  {
364  if ($isAlias) {
365  if (isset($this->_fieldsToSelect[$field])) {
366  unset($this->_fieldsToSelect[$field]);
367  $this->_fieldsToSelectChanged = true;
368  }
369  } else {
370  foreach ($this->_fieldsToSelect as $key => $value) {
371  if ($value === $field) {
372  unset($this->_fieldsToSelect[$key]);
373  $this->_fieldsToSelectChanged = true;
374  break;
375  }
376  }
377  }
378 
379  return $this;
380  }
$value
Definition: gender.phtml:16

◆ resetItemsDataChanged()

resetItemsDataChanged ( )

Set flag data has changed to all collection items

Returns
$this

Definition at line 556 of file AbstractCollection.php.

557  {
558  foreach ($this->_items as $item) {
559  $item->setDataChanges(false);
560  }
561 
562  return $this;
563  }

◆ save()

save ( )

Save all the entities in the collection

Returns
$this

Definition at line 591 of file AbstractCollection.php.

592  {
593  foreach ($this->getItems() as $item) {
594  $item->save();
595  }
596  return $this;
597  }

◆ setMainTable()

setMainTable (   $table)

Set main collection table

Parameters
string$table
Returns
$this

Definition at line 158 of file AbstractCollection.php.

159  {
160  $table = $this->getTable($table);
161  if ($this->_mainTable !== null && $table !== $this->_mainTable && $this->getSelect() !== null) {
162  $from = $this->getSelect()->getPart(\Magento\Framework\DB\Select::FROM);
163  if (isset($from['main_table'])) {
164  $from['main_table']['tableName'] = $table;
165  }
166  $this->getSelect()->setPart(\Magento\Framework\DB\Select::FROM, $from);
167  }
168 
169  $this->_mainTable = $table;
170  return $this;
171  }
$table
Definition: trigger.php:14

◆ setModel()

setModel (   $model)

Set model name for collection items

Parameters
string$model
Returns
$this

Definition at line 414 of file AbstractCollection.php.

415  {
416  if (is_string($model)) {
417  $this->_model = $model;
418  $this->setItemObjectClass($model);
419  }
420  return $this;
421  }

◆ setResetItemsDataChanged()

setResetItemsDataChanged (   $flag)

Set reset items data changed flag

Parameters
bool$flag
Returns
$this

Definition at line 545 of file AbstractCollection.php.

546  {
547  $this->_resetItemsDataChanged = (bool)$flag;
548  return $this;
549  }

◆ setResourceModel()

setResourceModel (   $model)

Set resource model name for collection items

Parameters
string$model
Returns
void

Definition at line 439 of file AbstractCollection.php.

440  {
441  $this->_resourceModel = $model;
442  }

Field Documentation

◆ $_eventManager

$_eventManager = null
protected

Definition at line 103 of file AbstractCollection.php.

◆ $_eventObject

$_eventObject = ''
protected

Definition at line 96 of file AbstractCollection.php.

◆ $_eventPrefix

$_eventPrefix = ''
protected

Definition at line 89 of file AbstractCollection.php.

◆ $_fieldsToSelect

$_fieldsToSelect = null
protected

Definition at line 47 of file AbstractCollection.php.

◆ $_fieldsToSelectChanged

$_fieldsToSelectChanged = false
protected

Definition at line 61 of file AbstractCollection.php.

◆ $_initialFieldsToSelect

$_initialFieldsToSelect = null
protected

Definition at line 54 of file AbstractCollection.php.

◆ $_joinedTables

$_joinedTables = []
protected

Definition at line 68 of file AbstractCollection.php.

◆ $_mainTable

$_mainTable = null
protected

Definition at line 75 of file AbstractCollection.php.

◆ $_model

$_model
protected

Definition at line 26 of file AbstractCollection.php.

◆ $_resetItemsDataChanged

$_resetItemsDataChanged = false
protected

Definition at line 82 of file AbstractCollection.php.

◆ $_resource

$_resource
protected

Definition at line 40 of file AbstractCollection.php.

◆ $_resourceModel

$_resourceModel
protected

Definition at line 33 of file AbstractCollection.php.


The documentation for this class was generated from the following file: