/** * Loads the messages from database. * You may override this method to customize the message storage in the database. * @param string $category the message category. * @param string $language the target language. * @return array the messages loaded from database. */ protected function loadMessagesFromDb($category, $language) { $mainQuery = new Query(); $mainQuery->select(['t1.message message', 't2.translation translation'])->from(["{$this->sourceMessageTable} t1", "{$this->messageTable} t2"])->where('t1.id = t2.id AND t1.category = :category AND t2.language = :language')->params([':category' => $category, ':language' => $language]); $fallbackLanguage = substr($language, 0, 2); if ($fallbackLanguage != $language) { $fallbackQuery = new Query(); $fallbackQuery->select(['t1.message message', 't2.translation translation'])->from(["{$this->sourceMessageTable} t1", "{$this->messageTable} t2"])->where('t1.id = t2.id AND t1.category = :category AND t2.language = :fallbackLanguage')->andWhere("t2.id NOT IN (SELECT id FROM {$this->messageTable} WHERE language = :language)")->params([':category' => $category, ':language' => $language, ':fallbackLanguage' => $fallbackLanguage]); $mainQuery->union($fallbackQuery, true); } $messages = $mainQuery->createCommand($this->db)->queryAll(); return ArrayHelper::map($messages, 'message', 'translation'); }
/** * Retrieves multiple values from cache with the specified keys. * @param array $keys a list of keys identifying the cached values * @return array a list of cached values indexed by the keys */ protected function getValues($keys) { if (empty($keys)) { return []; } $query = new Query(); $query->select(['id', 'data'])->from($this->cacheTable)->where(['id' => $keys])->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')'); if ($this->db->enableQueryCache) { $this->db->enableQueryCache = false; $rows = $query->createCommand($this->db)->queryAll(); $this->db->enableQueryCache = true; } else { $rows = $query->createCommand($this->db)->queryAll(); } $results = []; foreach ($keys as $key) { $results[$key] = false; } foreach ($rows as $row) { $results[$row['id']] = $row['data']; } return $results; }