/** * Add billing agreement filter on orders collection * * @param \Magento\Framework\Data\Collection\AbstractDb $orderCollection * @param string|int|array $agreementIds * @return $this */ public function addOrdersFilter(\Magento\Framework\Data\Collection\AbstractDb $orderCollection, $agreementIds) { $agreementIds = is_array($agreementIds) ? $agreementIds : [$agreementIds]; $orderIds = $this->getConnection()->fetchCol($this->getConnection()->select()->from(['pbao' => $this->getTable('paypal_billing_agreement_order')], ['order_id'])->where('pbao.agreement_id IN(?)', $agreementIds)); $orderCollection->getSelect()->where('main_table.entity_id IN (?)', $orderIds); return $this; }
/** * Returns list of columns from fulltext index (doesn't support more then one FTI per table) * * @param DbCollection $collection * @param string $indexTable * @return array */ protected function getFulltextIndexColumns(DbCollection $collection, $indexTable) { $indexes = $collection->getConnection()->getIndexList($indexTable); foreach ($indexes as $index) { if (strtoupper($index['INDEX_TYPE']) == 'FULLTEXT') { return $index['COLUMNS_LIST']; } } return []; }
public function testAddOrdersFilter() { $this->resourceConnectionMock->expects($this->exactly(2))->method('getConnection')->willReturn($this->connectionMock); $this->resourceConnectionMock->expects($this->once())->method('getTableName')->with('paypal_billing_agreement_order')->willReturn('pref_paypal_billing_agreement_order'); $this->connectionMock->expects($this->once())->method('select')->willReturn($this->selectMock); $this->selectMock->expects($this->once())->method('from')->with(['pbao' => 'pref_paypal_billing_agreement_order'], ['order_id'], null)->willReturnSelf(); $this->selectMock->expects($this->exactly(2))->method('where')->withConsecutive(['pbao.agreement_id IN(?)', [100]], ['main_table.entity_id IN (?)', [500]])->willReturnSelf(); $this->connectionMock->expects($this->once())->method('fetchCol')->with($this->selectMock, [])->willReturn([500]); $this->collectionMock->expects($this->once())->method('getSelect')->willReturn($this->selectMock); $this->assertEquals($this->agreementResource, $this->agreementResource->addOrdersFilter($this->collectionMock, 100)); }
/** * {@inheritdoc} */ public function process(DbCollection $collection, $extensibleEntityClass = null) { $extensibleEntityClass = $extensibleEntityClass ?: $collection->getItemObjectClass(); $joinDirectives = $this->getJoinDirectivesForType($extensibleEntityClass); foreach ($joinDirectives as $attributeCode => $directive) { /** @var JoinDataInterface $joinData */ $joinData = $this->joinProcessorHelper->getJoinDataInterface(); $joinData->setAttributeCode($attributeCode)->setReferenceTable($directive[Converter::JOIN_REFERENCE_TABLE])->setReferenceTableAlias($this->getReferenceTableAlias($attributeCode))->setReferenceField($directive[Converter::JOIN_REFERENCE_FIELD])->setJoinField($directive[Converter::JOIN_ON_FIELD]); $joinData->setSelectFields($this->joinProcessorHelper->getSelectFieldsMap($attributeCode, $directive[Converter::JOIN_FIELDS])); $collection->joinExtensionAttribute($joinData, $this); } }
/** * Add events log to a collection * The collection id field is used without corellation, so it must be unique. * DESC ordering by event will be added to the collection * * @param \Magento\Framework\Data\Collection\AbstractDb $collection * @param int $eventTypeId * @param int $eventSubjectId * @param int $subtype * @param array $skipIds * @return $this */ public function applyLogToCollection(\Magento\Framework\Data\Collection\AbstractDb $collection, $eventTypeId, $eventSubjectId, $subtype, $skipIds = []) { $idFieldName = $collection->getResource()->getIdFieldName(); $derivedSelect = $this->getConnection()->select()->from($this->getTable('report_event'), ['event_id' => new \Zend_Db_Expr('MAX(event_id)'), 'object_id'])->where('event_type_id = ?', (int) $eventTypeId)->where('subject_id = ?', (int) $eventSubjectId)->where('subtype = ?', (int) $subtype)->where('store_id IN(?)', $this->getCurrentStoreIds())->group('object_id'); if ($skipIds) { if (!is_array($skipIds)) { $skipIds = [(int) $skipIds]; } $derivedSelect->where('object_id NOT IN(?)', $skipIds); } $collection->getSelect()->joinInner(['evt' => new \Zend_Db_Expr("({$derivedSelect})")], "{$idFieldName} = evt.object_id", [])->order('evt.event_id ' . \Magento\Framework\DB\Select::SQL_DESC); return $this; }
public function testApply() { $filter = new Filter(); $filter->setValue('test'); $this->collectionAbstractDbMock->expects($this->any())->method('getMainTable')->willReturn('testTable'); $this->collectionAbstractDbMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); $this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock); $this->connectionMock->expects($this->once())->method('getIndexList')->willReturn([['INDEX_TYPE' => 'FULLTEXT', 'COLUMNS_LIST' => ['col1', 'col2']]]); $this->selectMock->expects($this->once())->method('getPart')->willReturn([]); $this->selectMock->expects($this->once())->method('where')->willReturn(null); $this->collectionAbstractDbMock->expects($this->exactly(2))->method('getSelect')->willReturn($this->selectMock); $this->fulltextFilter->apply($this->collectionAbstractDbMock, $filter); }
/** * Apply fulltext filters * * @param DbCollection $collection * @param array $filters * @return void */ public function apply(DbCollection $collection, $filters) { $columns = $this->getFulltextIndexColumns($collection->getResource(), $collection->getMainTable()); if (!$columns) { return; } foreach ($filters as $filter) { $collection->getSelect() ->where( 'MATCH(' . implode(',', $columns) . ') AGAINST(?)', $filter['condition'] ); } }
/** * Add table alias to columns * * @param array $columns * @param AbstractDb $collection * @param string $indexTable * @return array */ protected function addTableAliasToColumns(array $columns, AbstractDb $collection, $indexTable) { $alias = ''; foreach ($collection->getSelect()->getPart('from') as $tableAlias => $data) { if ($indexTable == $data['tableName']) { $alias = $tableAlias; break; } } if ($alias) { $columns = array_map(function ($column) use($alias) { return '`' . $alias . '`.' . $column; }, $columns); } return $columns; }
public function testToOptionHash() { $data = [10 => 'test']; $adapterMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', ['select', 'query'], [], '', false); $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], ['adapter' => $adapterMock]); $adapterMock->expects($this->once())->method('select')->will($this->returnValue($selectMock)); $this->fetchStrategyMock->expects($this->once())->method('fetchAll')->with($selectMock, [])->will($this->returnValue([$data])); $objectMock = $this->getMock('Magento\\Framework\\DataObject', ['addData', 'setIdFieldName', 'getData'], []); $objectMock->expects($this->once())->method('addData')->with($data); $objectMock->expects($this->any())->method('getData')->will($this->returnValueMap([[null, null, 10], ['name', null, 'test']])); $this->entityFactoryMock->expects($this->once())->method('create')->with('Magento\\Framework\\DataObject')->will($this->returnValue($objectMock)); $this->collection->setConnection($adapterMock); $this->collection->loadData(false, false); $this->collection->loadData(false, false); $this->assertEquals($data, $this->collection->toOptionHash()); }
/** * Search collection * * @return \Magento\Framework\Data\Collection\AbstractDb */ public function getSearchCollection() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */ $storeManager = $objectManager->get('Magento\\Store\\Model\\StoreManagerInterface'); /** @var \Magento\Store\Model\App\Emulation $emulation */ $emulation = $objectManager->create('Magento\\Store\\Model\\App\\Emulation'); if (!$this->searchCollection) { $isEmulation = false; if ($this->getData('store_id') && $this->getData('store_id') != $storeManager->getStore()->getId()) { $emulation->startEnvironmentEmulation($this->getData('store_id')); $isEmulation = true; } $this->searchCollection = $this->buildSearchCollection(); if ($isEmulation) { $this->searchCollection->getSize(); // get size before switch to default store $emulation->stopEnvironmentEmulation(); } } return $this->searchCollection; }
/** * Remove item from collection by item key * * @param mixed $key * @return $this */ public function removeItemByKey($key) { if (isset($this->_items[$key])) { unset($this->_itemsById[$this->_items[$key]->getId()]); } return parent::removeItemByKey($key); }
/** * @inheritdoc */ public function __wakeup() { parent::__wakeup(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->_eventManager = $objectManager->get(\Magento\Framework\Event\ManagerInterface::class); }
/** * Retrieve number of loaded stores * * @return int */ public function countStores() { return sizeof($this->_stores->getItems()); }
/** * Apply regular filters like collection filters * * @param AbstractDb $collection * @param Filter $filter * @return void */ public function apply(AbstractDb $collection, Filter $filter) { $collection->addFieldToFilter($filter->getField(), [$filter->getConditionType() => $filter->getValue()]); }
/** * @param AbstractDb $collection * @return AbstractDb * @throws LocalizedException */ protected function applySelection(AbstractDb $collection) { $selected = $this->request->getParam(static::SELECTED_PARAM); $excluded = $this->request->getParam(static::EXCLUDED_PARAM); if ('false' === $excluded) { return $collection; } try { if (is_array($excluded) && !empty($excluded)) { $collection->addFieldToFilter($collection->getIdFieldName(), ['nin' => $excluded]); } elseif (is_array($selected) && !empty($selected)) { $collection->addFieldToFilter($collection->getIdFieldName(), ['in' => $selected]); } else { throw new LocalizedException(__('Please select item(s).')); } } catch (\Exception $e) { throw new LocalizedException(__($e->getMessage())); } return $collection; }
/** * Join matches to collection * * @param AbstractDb $collection * @param string $field * * @return $this */ public function joinMatches($collection, $field = 'e.entity_id') { $requestBuilder = $this->requestBuilderFactory->create(); $queryText = $this->queryFactory->get()->getQueryText(); $requestBuilder->bind('search_term', $queryText); $requestBuilder->bindDimension('scope', $this->scopeResolver->getScope()); $requestBuilder->setRequestName($this->index->getCode()); $queryRequest = $requestBuilder->create(); $queryResponse = $this->searchEngine->search($queryRequest); $temporaryStorage = $this->temporaryStorageFactory->create(); if ($field == 'ID') { //external connection (need improve detection) $ids = [0]; foreach ($queryResponse->getIterator() as $item) { $ids[] = $item->getId(); } $collection->getSelect()->where(new \Zend_Db_Expr("{$field} IN (" . implode(',', $ids) . ")")); } else { $table = $temporaryStorage->storeDocuments($queryResponse->getIterator()); $collection->getSelect()->joinInner(['search_result' => $table->getName()], $field . ' = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []); } return $this; }
/** * Apply regular filters like collection filters * * @param AbstractDb $collection * @param array $filters * @return void */ public function apply(AbstractDb $collection, $filters) { foreach ($filters as $filter) { $collection->addFieldToFilter($filter['field'], $filter['condition']); } }
/** * Redeclare after load method for specifying collection items original data * * @return $this */ protected function _afterLoad() { parent::_afterLoad(); foreach ($this->_items as $item) { $item->setOrigData(); if ($this->_resetItemsDataChanged && $item instanceof \Magento\Framework\Model\AbstractModel) { $item->setDataChanges(false); } } $this->_eventManager->dispatch('core_collection_abstract_load_after', ['collection' => $this]); if ($this->_eventPrefix && $this->_eventObject) { $this->_eventManager->dispatch($this->_eventPrefix . '_load_after', [$this->_eventObject => $this]); } return $this; }
/** * Add billing agreement filter on orders collection * * @param \Magento\Framework\Data\Collection\AbstractDb $orderCollection * @param string|int|array $agreementIds * @return $this */ public function addOrdersFilter(\Magento\Framework\Data\Collection\AbstractDb $orderCollection, $agreementIds) { $agreementIds = is_array($agreementIds) ? $agreementIds : [$agreementIds]; $orderCollection->getSelect()->joinInner(['pbao' => $this->getTable('paypal_billing_agreement_order')], 'main_table.entity_id = pbao.order_id', [])->where('pbao.agreement_id IN(?)', $agreementIds); return $this; }