exists() публичный Метод

{@inheritDoc}
public exists ( $conditions )
Пример #1
0
 public function exists($conditions, $checkStatus = false)
 {
     if (!is_array($conditions) && is_numeric($conditions)) {
         $conditions = ['id' => $conditions];
     }
     if ($checkStatus && !empty($this->_activeStatuses)) {
         $conditions['status IN '] = $this->_activeStatuses;
     }
     return parent::exists($conditions);
 }
Пример #2
0
 /**
  * 2.x shim for exists() and primary key.
  *
  * @param int $id
  * @return bool
  */
 public function existsById($id)
 {
     $conditions = [$this->primaryKey() => $id];
     return parent::exists($conditions);
 }
Пример #3
0
 /**
  * Slug method
  *
  * For the given string, generate a slug. The replacements used are based on the mode setting, If tidy is false
  * (only possible if directly called - primarily for tracing and testing) separators will not be cleaned up
  * and so slugs like "-----as---df-----" are possible, which by default would otherwise be returned as "as-df".
  * If the mode is "id" and the first charcter of the regex-ed slug is numeric, it will be prefixed with an x.
  * If unique is set to true, check for a unique slug and if unavailable suffix the slug with -1, -2, -3 etc.
  * until a unique slug is found
  *
  * @param string $value
  * @param \Cake\ORM\Entity|null $entity
  * @return string A slug
  */
 public function generateSlug($value, Entity $entity = null)
 {
     $separator = $this->_config['separator'];
     $string = str_replace(["\r\n", "\r", "\n"], ' ', $value);
     $replace = $this->_config['replace'];
     if ($replace) {
         $string = str_replace(array_keys($replace), array_values($replace), $string);
     }
     if ($this->_config['mode'] === 'ascii') {
         $slug = Inflector::slug($string, $separator);
     } else {
         $regex = $this->_regex($this->_config['mode']);
         if ($regex) {
             $slug = $this->_pregReplace('@[' . $regex . ']@Su', $separator, $string);
         } else {
             $slug = $string;
         }
     }
     if ($this->_config['tidy']) {
         $slug = $this->_pregReplace('/' . $separator . '+/', $separator, $slug);
         $slug = trim($slug, $separator);
         if ($slug && $this->_config['mode'] === 'id' && is_numeric($slug[0])) {
             $slug = 'x' . $slug;
         }
     }
     if ($this->_config['length'] && mb_strlen($slug) > $this->_config['length']) {
         $slug = mb_substr($slug, 0, $this->_config['length']);
     }
     if ($this->_config['case']) {
         $case = $this->_config['case'];
         if ($case === 'up') {
             $slug = mb_strtoupper($slug);
         } else {
             $slug = mb_strtolower($slug);
         }
         if (in_array($case, ['title', 'camel'])) {
             $words = explode($separator, $slug);
             foreach ($words as $i => &$word) {
                 $firstChar = mb_substr($word, 0, 1);
                 $rest = mb_substr($word, 1, mb_strlen($word) - 1);
                 $firstCharUp = mb_strtoupper($firstChar);
                 $word = $firstCharUp . $rest;
             }
             if ($case === 'title') {
                 $slug = implode($words, $separator);
             } elseif ($case === 'camel') {
                 $slug = implode($words);
             }
         }
     }
     if ($this->_config['unique']) {
         if (!$entity) {
             throw new Exception('Needs an Entity to work on');
         }
         $field = $this->_table->alias() . '.' . $this->_config['field'];
         $conditions = [$field => $slug];
         $conditions = array_merge($conditions, $this->_config['scope']);
         $id = $entity->get($this->_table->primaryKey());
         if ($id) {
             $conditions['NOT'][$this->_table->alias() . '.' . $this->_table->primaryKey()] = $id;
         }
         $i = 0;
         $suffix = '';
         while ($this->_table->exists($conditions)) {
             $i++;
             $suffix = $separator . $i;
             if ($this->_config['length'] && mb_strlen($slug . $suffix) > $this->_config['length']) {
                 $slug = mb_substr($slug, 0, $this->_config['length'] - mb_strlen($suffix));
             }
             $conditions[$field] = $slug . $suffix;
         }
         if ($suffix) {
             $slug .= $suffix;
         }
     }
     return $slug;
 }