framework/db/BaseActiveRecord.php
public function unlinkAll($name, $delete = false)
    {
        $relation = $this->getRelation($name);

        if ($relation->via !== null) {
            if (is_array($relation->via)) {
                /* @var $viaRelation ActiveQuery */
                list($viaName, $viaRelation) = $relation->via;
                $viaClass = $viaRelation->modelClass;
                unset($this->_related[$viaName]);
            } else {
                $viaRelation = $relation->via;
                $viaTable = reset($relation->via->from);
            }
            $condition = [];
            $nulls = [];
            foreach ($viaRelation->link as $a => $b) {
                $nulls[$a] = null;
                $condition[$a] = $this->$b;
            }
            if (!empty($viaRelation->where)) {
                $condition = ['and', $condition, $viaRelation->where];
            }
            if (!empty($viaRelation->on)) {
                $condition = ['and', $condition, $viaRelation->on];
            }
            if (is_array($relation->via)) {
                /* @var $viaClass ActiveRecordInterface */
                if ($delete) {
                    $viaClass::deleteAll($condition);
                } else {
                    $viaClass::updateAll($nulls, $condition);
                }
            } else {
                /* @var $viaTable string */
                /* @var $command Command */
                $command = static::getDb()->createCommand();
                if ($delete) {
                    $command->delete($viaTable, $condition)->execute();
                } else {
                    $command->update($viaTable, $nulls, $condition)->execute();
                }
            }
        } else {
            /* @var $relatedModel ActiveRecordInterface */
            $relatedModel = $relation->modelClass;
            if (!$delete && count($relation->link) === 1 && is_array($this->{$b = reset($relation->link)})) {
                // relation via array valued attribute
                $this->$b = [];
                $this->save(false);
            } else {
                $nulls = [];
                $condition = [];
                foreach ($relation->link as $a => $b) {
                    $nulls[$a] = null;
                    $condition[$a] = $this->$b;
                }
                if (!empty($relation->where)) {
                    $condition = ['and', $condition, $relation->where];
                }
                if (!empty($relation->on)) {
                    $condition = ['and', $condition, $relation->on];
                }
                if ($delete) {
                    $relatedModel::deleteAll($condition);
                } else {
                    $relatedModel::updateAll($nulls, $condition);
                }
            }
        }

        unset($this->_related[$name]);
    }
public function unlinkAll($name, $delete = false)
    {
        $relation = $this->getRelation($name);

        if ($relation->via !== null) {
            if (is_array($relation->via)) {
                /* @var $viaRelation ActiveQuery */
                list($viaName, $viaRelation) = $relation->via;
                $viaClass = $viaRelation->modelClass;
                unset($this->_related[$viaName]);
            } else {
                $viaRelation = $relation->via;
                $viaTable = reset($relation->via->from);
            }
            $condition = [];
            $nulls = [];
            foreach ($viaRelation->link as $a => $b) {
                $nulls[$a] = null;
                $condition[$a] = $this->$b;
            }
            if (!empty($viaRelation->where)) {
                $condition = ['and', $condition, $viaRelation->where];
            }
            if (!empty($viaRelation->on)) {
                $condition = ['and', $condition, $viaRelation->on];
            }
            if (is_array($relation->via)) {
                /* @var $viaClass ActiveRecordInterface */
                if ($delete) {
                    $viaClass::deleteAll($condition);
                } else {
                    $viaClass::updateAll($nulls, $condition);
                }
            } else {
                /* @var $viaTable string */
                /* @var $command Command */
                $command = static::getDb()->createCommand();
                if ($delete) {
                    $command->delete($viaTable, $condition)->execute();
                } else {
                    $command->update($viaTable, $nulls, $condition)->execute();
                }
            }
        } else {
            /* @var $relatedModel ActiveRecordInterface */
            $relatedModel = $relation->modelClass;
            if (!$delete && count($relation->link) === 1 && is_array($this->{$b = reset($relation->link)})) {
                // relation via array valued attribute
                $this->$b = [];
                $this->save(false);
            } else {
                $nulls = [];
                $condition = [];
                foreach ($relation->link as $a => $b) {
                    $nulls[$a] = null;
                    $condition[$a] = $this->$b;
                }
                if (!empty($relation->where)) {
                    $condition = ['and', $condition, $relation->where];
                }
                if (!empty($relation->on)) {
                    $condition = ['and', $condition, $relation->on];
                }
                if ($delete) {
                    $relatedModel::deleteAll($condition);
                } else {
                    $relatedModel::updateAll($nulls, $condition);
                }
            }
        }

        unset($this->_related[$name]);
    }
public function unlinkAll ( $ name , $ delete = false ) { $ relation = $ this -> getRelation ( $ name ) ; if ( $ relation -> via !== null ) { if ( is_array ( $ relation -> via ) ) { /* @var $viaRelation ActiveQuery */ list ( $ viaName , $ viaRelation ) = $ relation -> via ; $ viaClass = $ viaRelation -> modelClass ; unset ( $ this -> _related [ $ viaName ] ) ; } else { $ viaRelation = $ relation -> via ; $ viaTable = reset ( $ relation -> via -> from ) ; } $ condition = [ ] ; $ nulls = [ ] ; foreach ( $ viaRelation -> link as $ a => $ b ) { $ nulls [ $ a ] = null ; $ condition [ $ a ] = $ this -> $ b ; } if ( ! empty ( $ viaRelation -> where ) ) { $ condition = [ 'and' , $ condition , $ viaRelation -> where ] ; } if ( ! empty ( $ viaRelation -> on ) ) { $ condition = [ 'and' , $ condition , $ viaRelation -> on ] ; } if ( is_array ( $ relation -> via ) ) { /* @var $viaClass ActiveRecordInterface */ if ( $ delete ) { $ viaClass :: deleteAll ( $ condition ) ; } else { $ viaClass :: updateAll ( $ nulls , $ condition ) ; } } else { /* @var $viaTable string */ /* @var $command Command */ $ command = static :: getDb ( ) -> createCommand ( ) ; if ( $ delete ) { $ command -> delete ( $ viaTable , $ condition ) -> execute ( ) ; } else { $ command -> update ( $ viaTable , $ nulls , $ condition ) -> execute ( ) ; } } } else { /* @var $relatedModel ActiveRecordInterface */ $ relatedModel = $ relation -> modelClass ; if ( ! $ delete && count ( $ relation -> link ) === 1 && is_array ( $ this -> { $ b = reset ( $ relation -> link ) } ) ) { // relation via array valued attribute $ this -> $ b = [ ] ; $ this -> save ( false ) ; } else { $ nulls = [ ] ; $ condition = [ ] ; foreach ( $ relation -> link as $ a => $ b ) { $ nulls [ $ a ] = null ; $ condition [ $ a ] = $ this -> $ b ; } if ( ! empty ( $ relation -> where ) ) { $ condition = [ 'and' , $ condition , $ relation -> where ] ; } if ( ! empty ( $ relation -> on ) ) { $ condition = [ 'and' , $ condition , $ relation -> on ] ; } if ( $ delete ) { $ relatedModel :: deleteAll ( $ condition ) ; } else { $ relatedModel :: updateAll ( $ nulls , $ condition ) ; } } } unset ( $ this -> _related [ $ name ] ) ; }
Destroys the relationship in current model.

The model with the foreign key of the relationship will be deleted if `$delete` is `true`.
Otherwise, the foreign key will be set `null` and the model will be saved without validation.

Note that to destroy the relationship without removing records make sure your keys can be set to null

@param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method.
@param bool $delete whether to delete the model that contains the foreign key.

Note that the deletion will be performed using [[deleteAll()]], which will not trigger any events on the related models.
If you need [[EVENT_BEFORE_DELETE]] or [[EVENT_AFTER_DELETE]] to be triggered, you need to [[find()|find]] the models first
and then call [[delete()]] on each of them.
Destroys the relationship in current model .