protected function _slug($string, $entity)
    {
        $string = $this->_mbTrim(mb_strtolower($string));
        $config = $this->config();
        $slug = Inflector::slug($string, $config['separator']);
        $pk = $this->_table->primaryKey();

        if (mb_strlen($slug) > $config['length']) {
            $slug = mb_substr($slug, 0, $config['length']);
        }

        $conditions = ["{$config['slug']} LIKE" => "{$slug}%"];
        if ($entity->has($pk)) {
            $conditions["{$pk} NOT IN"] = [$entity->{$pk}];
        }

        $same = $this->_table->find()
            ->where($conditions)
            ->all()
            ->extract($config['slug'])
            ->toArray();

        if (!empty($same)) {
            $initialSlug = $slug;
            $index = 1;
            while ($index > 0) {
                $nextSlug = "{$initialSlug}{$config['separator']}{$index}";
                if (!in_array($nextSlug, $same)) {
                    $slug = $nextSlug;
                    $index = -1;
                }

                $index++;
            }
        }

        return $slug;
    }
protected function _slug($string, $entity)
    {
        $string = $this->_mbTrim(mb_strtolower($string));
        $config = $this->config();
        $slug = Inflector::slug($string, $config['separator']);
        $pk = $this->_table->primaryKey();

        if (mb_strlen($slug) > $config['length']) {
            $slug = mb_substr($slug, 0, $config['length']);
        }

        $conditions = ["{$config['slug']} LIKE" => "{$slug}%"];
        if ($entity->has($pk)) {
            $conditions["{$pk} NOT IN"] = [$entity->{$pk}];
        }

        $same = $this->_table->find()
            ->where($conditions)
            ->all()
            ->extract($config['slug'])
            ->toArray();

        if (!empty($same)) {
            $initialSlug = $slug;
            $index = 1;
            while ($index > 0) {
                $nextSlug = "{$initialSlug}{$config['separator']}{$index}";
                if (!in_array($nextSlug, $same)) {
                    $slug = $nextSlug;
                    $index = -1;
                }

                $index++;
            }
        }

        return $slug;
    }
protected function _slug ( $ string , $ entity ) { $ string = $ this -> _mbTrim ( mb_strtolower ( $ string ) ) ; $ config = $ this -> config ( ) ; $ slug = Inflector :: slug ( $ string , $ config [ 'separator' ] ) ; $ pk = $ this -> _table -> primaryKey ( ) ; if ( mb_strlen ( $ slug ) > $ config [ 'length' ] ) { $ slug = mb_substr ( $ slug , 0 , $ config [ 'length' ] ) ; } $ conditions = [ "{$config['slug']} LIKE" => "{$slug}%" ] ; if ( $ entity -> has ( $ pk ) ) { $ conditions [ "{$pk} NOT IN" ] = [ $ entity -> { $ pk } ] ; } $ same = $ this -> _table -> find ( ) -> where ( $ conditions ) -> all ( ) -> extract ( $ config [ 'slug' ] ) -> toArray ( ) ; if ( ! empty ( $ same ) ) { $ initialSlug = $ slug ; $ index = 1 ; while ( $ index > 0 ) { $ nextSlug = "{$initialSlug}{$config['separator']}{$index}" ; if ( ! in_array ( $ nextSlug , $ same ) ) { $ slug = $ nextSlug ; $ index = - 1 ; } $ index ++ ; } } return $ slug ; }
Generate a slug for the given string and entity. The generated slug is
unique across the entire table.

@param string $string String from where to generate slug
@param \Cake\ORM\Entity $entity The entity for which generate the slug
@return string Slug for given string
Generate a slug for the given string and entity . The generated slug is unique across the entire table .