/** * Test that after cloning collection $this->_select in initial and cloned collections * do not reference the same object * * @covers \Magento\Framework\Data\Collection\Db::__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'); }
/** * 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\Db $collection * @param int $eventTypeId * @param int $eventSubjectId * @param int $subtype * @param array $skipIds * @return $this */ public function applyLogToCollection(\Magento\Framework\Data\Collection\Db $collection, $eventTypeId, $eventSubjectId, $subtype, $skipIds = []) { $idFieldName = $collection->getResource()->getIdFieldName(); $derivedSelect = $this->getReadConnection()->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; }
/** * 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(); }