/** * 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; }