public static function save($data, $name, $tags = null) { $caches_model = new Caches(); $cache_row = $caches_model->fetchRowArray($caches_model->select()->where('name = ?', $name)); if (empty($cache_row)) { $cache = array('name' => $name, 'data' => serialize($data), 'created_on' => date(DB_DATETIME_FORMAT)); try { $cache_id = $caches_model->insert($cache); } catch (Exception $e) { $cache_id = $caches_model->update($cache, $caches_model->select()->where('name = ?', $cache['name'])); } } else { $cache_id = $cache_row['id']; } if (!empty($tags) && is_array($tags)) { $cache_tags_model = new CacheTags(); $caches_tags_model = new CachesTags(); $tag_data = array(); foreach ($tags as $tag) { $tag_row = $cache_tags_model->fetchRowArray($cache_tags_model->select()->where('tag = ?', $tag)); if (empty($tag_row)) { $tag_data = array('tag' => $tag, 'created_on' => date(DB_DATETIME_FORMAT)); $tag_id = $cache_tags_model->insert($tag_data); } else { $tag_id = $tag_row['id']; } $tag_join_row = $caches_tags_model->fetchRowArray($caches_tags_model->select()->where('tag_id = ?', $tag_id)->where('cache_id = ?', $cache_id)); if (empty($tag_join_row)) { $tag_join_data = array('cache_id' => $cache_id, 'tag_id' => $tag_id, 'created_on' => date(DB_DATETIME_FORMAT)); $caches_tags_model->insert($tag_join_data); } } } }