/** * 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; }
/** * Test that after cloning collection $this->_select in initial and cloned collections * do not reference the same object * * @covers \Magento\Framework\Data\Collection\AbstractDb::__clone */ public function testClone() { $adapter = $this->getMockForAbstractClass('Zend_Db_Adapter_Abstract', [], '', false); $this->collection->setConnection($adapter); $this->assertInstanceOf('Zend_Db_Select', $this->collection->getSelect()); $clonedCollection = clone $this->collection; $this->assertInstanceOf('Zend_Db_Select', $clonedCollection->getSelect()); $this->assertNotSame($clonedCollection->getSelect(), $this->collection->getSelect(), 'Collection was cloned but $this->_select in both initial and cloned collections reference the same object'); }
/** * Test that after cloning collection $this->_select in initial and cloned collections * do not reference the same object * * @covers \Magento\Framework\Data\Collection\AbstractDb::__clone */ public function testClone() { $adapter = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', [], [], '', false); $adapter->expects($this->any())->method('select')->willReturn($this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false)); $this->collection->setConnection($adapter); $this->assertInstanceOf('Magento\\Framework\\DB\\Select', $this->collection->getSelect()); $clonedCollection = clone $this->collection; $this->assertInstanceOf('Magento\\Framework\\DB\\Select', $clonedCollection->getSelect()); $this->assertNotSame($clonedCollection->getSelect(), $this->collection->getSelect(), 'Collection was cloned but $this->_select in both initial and cloned collections reference the same object'); }
/** * 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; }
/** * 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; }
/** * Get \Zend_Db_Select instance and applies fields to select if needed * * @return \Magento\Framework\DB\Select */ public function getSelect() { if ($this->_select && $this->_fieldsToSelectChanged) { $this->_fieldsToSelectChanged = false; $this->_initSelectFields(); } return parent::getSelect(); }
/** * 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; }
/** * 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; }