Esempio n. 1
0
 public function testGet()
 {
     SlugCache::set('User', array(1 => 2));
     $result = SlugCache::get('User');
     $expected = array(1 => 2);
     $this->assertEquals($expected, $result);
     SlugCache::$_varCache = array();
     $result = SlugCache::get('User');
     $expected = array(1 => 2);
     $this->assertEquals($expected, $result);
     $result = SlugCache::$_varCache;
     $expected = array('SluggerTest' => array('User' => array(1 => 2)));
     $this->assertEquals($expected, $result);
 }
Esempio n. 2
0
 /**
  * Gets slugs, checks cache first
  *
  * @param string $modelName The name of the model
  * @return array Array of slugs
  */
 public function getSlugs($modelName)
 {
     $slugs = SlugCache::get($modelName);
     if (empty($slugs)) {
         $Model = ClassRegistry::init($modelName, true);
         if ($Model === false) {
             return false;
         }
         $field = $Model->displayField;
         if (!empty($this->models[$modelName]['slugField'])) {
             $field = $this->models[$modelName]['slugField'];
         }
         $slugs = $Model->find('all', array('fields' => array($Model->name . '.' . $Model->primaryKey, $Model->name . '.' . $field), 'recursive' => -1));
         $counts = $Model->find('all', array('fields' => array('LOWER(TRIM(' . $Model->name . '.' . $field . ')) AS ' . $field, 'COUNT(*) AS count'), 'group' => array($field)));
         $counts = Set::combine($counts, '{n}.0.' . $field, '{n}.0.count');
         $listedSlugs = array();
         foreach ($slugs as $pk => $fields) {
             $values = array('_field' => $fields[$Model->name][$field], '_count' => $counts[strtolower(trim($fields[$Model->name][$field]))], '_pk' => $fields[$Model->name][$Model->primaryKey]);
             $listedSlugs[$fields[$Model->name][$Model->primaryKey]] = $this->slug($values);
         }
         SlugCache::set($modelName, $listedSlugs);
     }
     return SlugCache::get($modelName);
 }