src/Model/Entity/TagAwareTrait.php
public function untag($tags = null)
    {
        if (empty($tags)) {
            return $this->_updateTags([], 'replace');
        }

        $table = TableRegistry::getTableLocator()->get($this->source());
        $behavior = $table->behaviors()->Tag;
        $assoc = $table->getAssociation($behavior->getConfig('tagsAlias'));
        $property = $assoc->getProperty();
        $id = $this->get($table->getPrimaryKey());
        $untags = $behavior->normalizeTags($tags);

        $tags = $this->get($property);
        if (!$tags) {
            $contain = [$behavior->getConfig('tagsAlias')];
            $tags = $table->get($id, compact('contain'))->get($property);
        }

        $tagsTable = $table->{$behavior->getConfig('tagsAlias')};
        $pk = $tagsTable->getPrimaryKey();
        $df = $tagsTable->getDisplayField();

        foreach ($tags as $k => $tag) {
            $tags[$k] = [
                $pk => $tag->{$pk},
                $df => $tag->{$df},
            ];
        }

        foreach ($untags as $untag) {
            foreach ($tags as $k => $tag) {
                if ((empty($untag[$pk]) || $tag[$pk] === $untag[$pk]) &&
                    (empty($untag[$df]) || $tag[$df] === $untag[$df])
                ) {
                    unset($tags[$k]);
                }
            }
        }

        return $this->_updateTags(
            array_map(
                function ($i) {
                    return implode(':', $i);
                },
                $tags
            ),
            'replace'
        );
    }
public function untag($tags = null)
    {
        if (empty($tags)) {
            return $this->_updateTags([], 'replace');
        }

        $table = TableRegistry::getTableLocator()->get($this->source());
        $behavior = $table->behaviors()->Tag;
        $assoc = $table->getAssociation($behavior->getConfig('tagsAlias'));
        $property = $assoc->getProperty();
        $id = $this->get($table->getPrimaryKey());
        $untags = $behavior->normalizeTags($tags);

        $tags = $this->get($property);
        if (!$tags) {
            $contain = [$behavior->getConfig('tagsAlias')];
            $tags = $table->get($id, compact('contain'))->get($property);
        }

        $tagsTable = $table->{$behavior->getConfig('tagsAlias')};
        $pk = $tagsTable->getPrimaryKey();
        $df = $tagsTable->getDisplayField();

        foreach ($tags as $k => $tag) {
            $tags[$k] = [
                $pk => $tag->{$pk},
                $df => $tag->{$df},
            ];
        }

        foreach ($untags as $untag) {
            foreach ($tags as $k => $tag) {
                if ((empty($untag[$pk]) || $tag[$pk] === $untag[$pk]) &&
                    (empty($untag[$df]) || $tag[$df] === $untag[$df])
                ) {
                    unset($tags[$k]);
                }
            }
        }

        return $this->_updateTags(
            array_map(
                function ($i) {
                    return implode(':', $i);
                },
                $tags
            ),
            'replace'
        );
    }
public function untag ( $ tags = null ) { if ( empty ( $ tags ) ) { return $ this -> _updateTags ( [ ] , 'replace' ) ; } $ table = TableRegistry :: getTableLocator ( ) -> get ( $ this -> source ( ) ) ; $ behavior = $ table -> behaviors ( ) -> Tag ; $ assoc = $ table -> getAssociation ( $ behavior -> getConfig ( 'tagsAlias' ) ) ; $ property = $ assoc -> getProperty ( ) ; $ id = $ this -> get ( $ table -> getPrimaryKey ( ) ) ; $ untags = $ behavior -> normalizeTags ( $ tags ) ; $ tags = $ this -> get ( $ property ) ; if ( ! $ tags ) { $ contain = [ $ behavior -> getConfig ( 'tagsAlias' ) ] ; $ tags = $ table -> get ( $ id , compact ( 'contain' ) ) -> get ( $ property ) ; } $ tagsTable = $ table -> { $ behavior -> getConfig ( 'tagsAlias' ) } ; $ pk = $ tagsTable -> getPrimaryKey ( ) ; $ df = $ tagsTable -> getDisplayField ( ) ; foreach ( $ tags as $ k => $ tag ) { $ tags [ $ k ] = [ $ pk => $ tag -> { $ pk } , $ df => $ tag -> { $ df } , ] ; } foreach ( $ untags as $ untag ) { foreach ( $ tags as $ k => $ tag ) { if ( ( empty ( $ untag [ $ pk ] ) || $ tag [ $ pk ] === $ untag [ $ pk ] ) && ( empty ( $ untag [ $ df ] ) || $ tag [ $ df ] === $ untag [ $ df ] ) ) { unset ( $ tags [ $ k ] ) ; } } } return $ this -> _updateTags ( array_map ( function ( $ i ) { return implode ( ':' , $ i ) ; } , $ tags ) , 'replace' ) ; }
Untag entity from given tags.

@param string|array $tags List of tags as an array or a delimited string (comma by default).
If no value is passed all tags will be removed.
@return bool|\Cake\ORM\Entity False on failure, entity on success.
Untag entity from given tags .