Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Collection Class Reference
Inheritance diagram for Collection:
AbstractCollection AbstractDb SourceProviderInterface Collection ArrayInterface CollectionDataSourceInterface OptionSourceInterface ArgumentInterface Collection Grid

Public Member Functions

 __construct (\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration, \Magento\Sales\Helper\Admin $adminhtmlSales, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Wishlist\Model\Config $wishlistConfig, \Magento\Catalog\Model\Product\Visibility $productVisibility, \Magento\Framework\App\ResourceConnection $coreResource, \Magento\Wishlist\Model\ResourceModel\Item\Option\CollectionFactory $optionCollectionFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\ResourceModel\ConfigFactory $catalogConfFactory, \Magento\Catalog\Model\Entity\AttributeFactory $catalogAttrFactory, \Magento\Wishlist\Model\ResourceModel\Item $resource, \Magento\Framework\App\State $appState, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null)
 
 _construct ()
 
 addWishlistFilter (\Magento\Wishlist\Model\Wishlist $wishlist)
 
 addCustomerIdFilter ($customerId)
 
 addStoreFilter ($storeIds=[])
 
 addStoreData ()
 
 resetSortOrder ()
 
 setVisibilityFilter ($flag=true)
 
 setSalableFilter ($flag=true)
 
 setInStockFilter ($flag=true)
 
 addDaysInWishlist ()
 
 addDaysFilter ($constraints)
 
 addProductNameFilter ($productName)
 
 setOrderByProductName ($dir)
 
 getItemsQty ()
 
- Public Member Functions inherited from AbstractCollection
 __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

 getMetadataPool ()
 
 _afterLoad ()
 
 _assignOptions ()
 
 _afterLoadData ()
 
- Protected Member Functions inherited from AbstractCollection
 _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

 $_productVisible = false
 
 $_productSalable = false
 
 $_productInStock = false
 
 $_productIds = []
 
 $_storeIds = []
 
 $_addDaysInWishlist = false
 
 $_itemsQty
 
 $_isProductNameJoined = false
 
 $_adminhtmlSales = null
 
 $stockConfiguration = null
 
 $_storeManager
 
 $_date
 
 $_wishlistConfig
 
 $_productVisibility
 
 $_coreResource
 
 $_optionCollectionFactory
 
 $_productCollectionFactory
 
 $_catalogConfFactory
 
 $_catalogAttrFactory
 
 $_appState
 
 $metadataPool
 
- Protected Attributes inherited from AbstractCollection
 $_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

Wishlist item collection @SuppressWarnings(PHPMD.TooManyFields) @SuppressWarnings(PHPMD.CouplingBetweenObjects)

@api

Since
100.0.2

Definition at line 19 of file Collection.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Data\Collection\EntityFactory  $entityFactory,
\Psr\Log\LoggerInterface  $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface  $fetchStrategy,
\Magento\Framework\Event\ManagerInterface  $eventManager,
\Magento\CatalogInventory\Api\StockConfigurationInterface  $stockConfiguration,
\Magento\Sales\Helper\Admin  $adminhtmlSales,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Framework\Stdlib\DateTime\DateTime  $date,
\Magento\Wishlist\Model\Config  $wishlistConfig,
\Magento\Catalog\Model\Product\Visibility  $productVisibility,
\Magento\Framework\App\ResourceConnection  $coreResource,
\Magento\Wishlist\Model\ResourceModel\Item\Option\CollectionFactory  $optionCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory  $productCollectionFactory,
\Magento\Catalog\Model\ResourceModel\ConfigFactory  $catalogConfFactory,
\Magento\Catalog\Model\Entity\AttributeFactory  $catalogAttrFactory,
\Magento\Wishlist\Model\ResourceModel\Item  $resource,
\Magento\Framework\App\State  $appState,
\Magento\Framework\DB\Adapter\AdapterInterface  $connection = null 
)
Parameters
\Magento\Framework\Data\Collection\EntityFactory$entityFactory
\Psr\Log\LoggerInterface$logger
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface$fetchStrategy
\Magento\Framework\Event\ManagerInterface$eventManager
\Magento\CatalogInventory\Api\StockConfigurationInterface$stockConfiguration
\Magento\Sales\Helper\Admin$adminhtmlSales
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\Stdlib\DateTime\DateTime$date
\Magento\Wishlist\Model\Config$wishlistConfig
\Magento\Catalog\Model\Product\Visibility$productVisibility
\Magento\Framework\App\ResourceConnection$coreResource
\Magento\Wishlist\Model\ResourceModel\Item\Option\CollectionFactory$optionCollectionFactory
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory$productCollectionFactory
\Magento\Catalog\Model\ResourceModel\ConfigFactory$catalogConfFactory
\Magento\Catalog\Model\Entity\AttributeFactory$catalogAttrFactory
\Magento\Wishlist\Model\ResourceModel\Item$resource
\Magento\Framework\App\State$appState
\Magento\Framework\DB\Adapter\AdapterInterface$connection@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 169 of file Collection.php.

188  {
189  $this->stockConfiguration = $stockConfiguration;
190  $this->_adminhtmlSales = $adminhtmlSales;
191  $this->_storeManager = $storeManager;
192  $this->_date = $date;
193  $this->_wishlistConfig = $wishlistConfig;
194  $this->_productVisibility = $productVisibility;
195  $this->_coreResource = $coreResource;
196  $this->_optionCollectionFactory = $optionCollectionFactory;
197  $this->_productCollectionFactory = $productCollectionFactory;
198  $this->_catalogConfFactory = $catalogConfFactory;
199  $this->_catalogAttrFactory = $catalogAttrFactory;
200  $this->_appState = $appState;
201  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
202  }
$storeManager
$resource
Definition: bulk.php:12
$logger
$connection
Definition: bulk.php:13

Member Function Documentation

◆ _afterLoad()

_afterLoad ( )
protected

After load processing

Returns
$this

Assign products

Definition at line 235 of file Collection.php.

236  {
237  parent::_afterLoad();
238 
242  $this->_assignOptions();
243  $this->_assignProducts();
244  $this->resetItemsDataChanged();
245 
246  $this->getPageSize();
247 
248  return $this;
249  }

◆ _afterLoadData()

_afterLoadData ( )
protected

After load data

Returns
$this

Definition at line 577 of file Collection.php.

578  {
579  parent::_afterLoadData();
580 
581  if ($this->_addDaysInWishlist) {
582  $gmtOffset = (int)$this->_date->getGmtOffset();
583  $nowTimestamp = $this->_date->timestamp();
584 
585  foreach ($this as $wishlistItem) {
586  $wishlistItemTimestamp = $this->_date->timestamp($wishlistItem->getAddedAt());
587 
588  $wishlistItem->setDaysInWishlist(
589  (int)(($nowTimestamp - $gmtOffset - $wishlistItemTimestamp) / 24 / 60 / 60)
590  );
591  }
592  }
593 
594  return $this;
595  }

◆ _assignOptions()

_assignOptions ( )
protected

Add options to items

Returns
$this

Definition at line 256 of file Collection.php.

257  {
258  $itemIds = array_keys($this->_items);
259  /* @var $optionCollection \Magento\Wishlist\Model\ResourceModel\Item\Option\Collection */
260  $optionCollection = $this->_optionCollectionFactory->create();
261  $optionCollection->addItemFilter($itemIds);
262 
263  /* @var $item \Magento\Wishlist\Model\Item */
264  foreach ($this as $item) {
265  $item->setOptions($optionCollection->getOptionsByItem($item));
266  }
267  $productIds = $optionCollection->getProductIds();
268  $this->_productIds = array_merge($this->_productIds, $productIds);
269 
270  return $this;
271  }

◆ _construct()

_construct ( )

Initialize resource model for collection

Returns
void

Definition at line 209 of file Collection.php.

210  {
211  $this->_init(\Magento\Wishlist\Model\Item::class, \Magento\Wishlist\Model\ResourceModel\Item::class);
212  $this->addFilterToMap('store_id', 'main_table.store_id');
213  }
addFilterToMap($filter, $alias, $group='fields')
Definition: AbstractDb.php:798

◆ addCustomerIdFilter()

addCustomerIdFilter (   $customerId)

Add filtration by customer id

Parameters
int$customerId
Returns
$this

Definition at line 351 of file Collection.php.

352  {
353  $this->getSelect()->join(
354  ['wishlist' => $this->getTable('wishlist')],
355  'main_table.wishlist_id = wishlist.wishlist_id',
356  []
357  )->where(
358  'wishlist.customer_id = ?',
360  );
361  return $this;
362  }

◆ addDaysFilter()

addDaysFilter (   $constraints)

Adds filter on days in wishlist

The $constraints may contain 'from' and 'to' indexes with number of days to look for items

Parameters
array$constraints
Returns
$this

Definition at line 467 of file Collection.php.

468  {
469  if (!is_array($constraints)) {
470  return $this;
471  }
472 
473  $filter = [];
474 
475  $gmtOffset = (new \DateTimeZone(date_default_timezone_get()))->getOffset(new \DateTime());
476  if (isset($constraints['from'])) {
477  $lastDay = new \DateTime();
478  $lastDay->modify('-' . $gmtOffset . ' second')->modify('-' . $constraints['from'] . ' day');
479  $filter['to'] = $lastDay;
480  }
481 
482  if (isset($constraints['to'])) {
483  $firstDay = new \DateTime();
484  $firstDay->modify('-' . $gmtOffset . ' second')->modify('-' . (intval($constraints['to']) + 1) . ' day');
485  $filter['from'] = $firstDay;
486  }
487 
488  if ($filter) {
489  $filter['datetime'] = true;
490  $this->addFieldToFilter('added_at', $filter);
491  }
492 
493  return $this;
494  }

◆ addDaysInWishlist()

addDaysInWishlist ( )

Set flag of adding days in wishlist

Returns
$this

Definition at line 453 of file Collection.php.

454  {
455  $this->_addDaysInWishlist = true;
456  return $this;
457  }

◆ addProductNameFilter()

addProductNameFilter (   $productName)

Adds filter on product name

Parameters
string$productName
Returns
$this

Definition at line 533 of file Collection.php.

534  {
535  $this->_joinProductNameTable();
536  $this->getSelect()->where('INSTR(product_name_table.value, ?)', $productName);
537 
538  return $this;
539  }

◆ addStoreData()

addStoreData ( )

Add items store data to collection

Returns
$this

Definition at line 386 of file Collection.php.

387  {
388  $storeTable = $this->_coreResource->getTableName('store');
389  $this->getSelect()->join(
390  ['store' => $storeTable],
391  'main_table.store_id=store.store_id',
392  ['store_name' => 'name', 'item_store_id' => 'store_id']
393  );
394  return $this;
395  }

◆ addStoreFilter()

addStoreFilter (   $storeIds = [])

Add filter by shared stores

Parameters
array$storeIds
Returns
$this

Definition at line 370 of file Collection.php.

371  {
372  if (!is_array($storeIds)) {
373  $storeIds = [$storeIds];
374  }
375  $this->_storeIds = $storeIds;
376  $this->addFieldToFilter('store_id', ['in' => $this->_storeIds]);
377 
378  return $this;
379  }

◆ addWishlistFilter()

addWishlistFilter ( \Magento\Wishlist\Model\Wishlist  $wishlist)

Add filter by wishlist object

Parameters
\Magento\Wishlist\Model\Wishlist$wishlist
Returns
$this

Definition at line 339 of file Collection.php.

340  {
341  $this->addFieldToFilter('wishlist_id', $wishlist->getId());
342  return $this;
343  }
$wishlist
Definition: wishlist.php:10

◆ getItemsQty()

getItemsQty ( )

Get sum of items collection qty

Returns
int

Definition at line 559 of file Collection.php.

560  {
561  if ($this->_itemsQty === null) {
562  $this->_itemsQty = 0;
563  foreach ($this as $wishlistItem) {
564  $qty = $wishlistItem->getQty();
565  $this->_itemsQty += $qty === 0 ? 1 : $qty;
566  }
567  }
568 
569  return (int)$this->_itemsQty;
570  }

◆ getMetadataPool()

getMetadataPool ( )
protected

Get metadata pool object

Returns
MetadataPool
Since
100.1.0

Definition at line 221 of file Collection.php.

222  {
223  if ($this->metadataPool == null) {
225  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
226  }
227  return $this->metadataPool;
228  }

◆ resetSortOrder()

resetSortOrder ( )

Reset sort order

Returns
$this

Definition at line 402 of file Collection.php.

403  {
404  $this->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
405  return $this;
406  }
const ORDER
Definition: Select.php:54

◆ setInStockFilter()

setInStockFilter (   $flag = true)

Set In Stock Filter.

This filter remove items with no salable product.

Parameters
bool$flag
Returns
$this

Definition at line 442 of file Collection.php.

443  {
444  $this->_productInStock = (bool)$flag;
445  return $this;
446  }

◆ setOrderByProductName()

setOrderByProductName (   $dir)

Sets ordering by product name

Parameters
string$dir
Returns
$this

Definition at line 547 of file Collection.php.

548  {
549  $this->_joinProductNameTable();
550  $this->getSelect()->order('product_name_table.value ' . $dir);
551  return $this;
552  }

◆ setSalableFilter()

setSalableFilter (   $flag = true)

Set Salable Filter.

This filter apply Salable Product Types Filter to product collection.

Parameters
bool$flag
Returns
$this

Definition at line 428 of file Collection.php.

429  {
430  $this->_productSalable = (bool)$flag;
431  return $this;
432  }

◆ setVisibilityFilter()

setVisibilityFilter (   $flag = true)

Set product Visibility Filter to product collection flag

Parameters
bool$flag
Returns
$this

Definition at line 414 of file Collection.php.

415  {
416  $this->_productVisible = (bool)$flag;
417  return $this;
418  }

Field Documentation

◆ $_addDaysInWishlist

$_addDaysInWishlist = false
protected

Definition at line 61 of file Collection.php.

◆ $_adminhtmlSales

$_adminhtmlSales = null
protected

Definition at line 82 of file Collection.php.

◆ $_appState

$_appState
protected

Definition at line 139 of file Collection.php.

◆ $_catalogAttrFactory

$_catalogAttrFactory
protected

Definition at line 134 of file Collection.php.

◆ $_catalogConfFactory

$_catalogConfFactory
protected

Definition at line 129 of file Collection.php.

◆ $_coreResource

$_coreResource
protected

Definition at line 114 of file Collection.php.

◆ $_date

$_date
protected

Definition at line 99 of file Collection.php.

◆ $_isProductNameJoined

$_isProductNameJoined = false
protected

Definition at line 75 of file Collection.php.

◆ $_itemsQty

$_itemsQty
protected

Definition at line 68 of file Collection.php.

◆ $_optionCollectionFactory

$_optionCollectionFactory
protected

Definition at line 119 of file Collection.php.

◆ $_productCollectionFactory

$_productCollectionFactory
protected

Definition at line 124 of file Collection.php.

◆ $_productIds

$_productIds = []
protected

Definition at line 47 of file Collection.php.

◆ $_productInStock

$_productInStock = false
protected

Definition at line 40 of file Collection.php.

◆ $_productSalable

$_productSalable = false
protected

Definition at line 33 of file Collection.php.

◆ $_productVisibility

$_productVisibility
protected

Definition at line 109 of file Collection.php.

◆ $_productVisible

$_productVisible = false
protected

Definition at line 26 of file Collection.php.

◆ $_storeIds

$_storeIds = []
protected

Definition at line 54 of file Collection.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 94 of file Collection.php.

◆ $_wishlistConfig

$_wishlistConfig
protected

Definition at line 104 of file Collection.php.

◆ $metadataPool

$metadataPool
protected

Definition at line 145 of file Collection.php.

◆ $stockConfiguration

$stockConfiguration = null
protected

Definition at line 89 of file Collection.php.


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