Detach

@param $id
@param $relation
@param $attributes
@return mixed
971645
public function detach($id, $relation, $attributes)
    {
        return $this->find($id)->{$relation}()->detach($attributes);
    }
766140
public function find($id, $attributes = ['*'])
    {
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) {
            return $this->prepareQuery($this->createModel())->find($id, $attributes);
        });
    }
724857
public function getLinks($relation = null)
    {
        $links = $this->getAllLinks();
        if (null === $relation) {
            return $links;
        }
        foreach ($links as $key => $link) {
            if ($link->getRelation() !== $relation) {
                unset($links[$key]);
            }
        }

        return $links;
    }
1148437
public function detach($ids = null, $touch = true)
    {
        $query = $this->newPivotQuery();

        // If associated IDs were passed to the method we will only delete those
        // associations, otherwise all of the association ties will be broken.
        // We'll return the numbers of affected rows when we do the deletes.
        if (! is_null($ids = $this->parseIds($ids))) {
            if (count((array) $ids) === 0) {
                return 0;
            }

            $query->whereIn($this->relatedKey, (array) $ids);
        }

        // Once we have all of the conditions set on the statement, we are ready
        // to run the delete on the pivot table. Then, if the touch parameter
        // is true, we will go ahead and touch all related models to sync.
        foreach ($query->cursor() as $result) {
            $result->delete();
        }

        if ($touch) {
            $this->touchIfTouching();
        }

        return 1;
    }
379640
public function detach($ids = null, $touch = true)
    {
        if ($this->using && ! empty($ids) && empty($this->pivotWheres) && empty($this->pivotWhereIns)) {
            $results = $this->detachUsingCustomClass($ids);
        } else {
            $query = $this->newPivotQuery();

            // If associated IDs were passed to the method we will only delete those
            // associations, otherwise all of the association ties will be broken.
            // We'll return the numbers of affected rows when we do the deletes.
            if (! is_null($ids)) {
                $ids = $this->parseIds($ids);

                if (empty($ids)) {
                    return 0;
                }

                $query->whereIn($this->relatedPivotKey, (array) $ids);
            }

            // Once we have all of the conditions set on the statement, we are ready
            // to run the delete on the pivot table. Then, if the touch parameter
            // is true, we will go ahead and touch all related models to sync.
            $results = $query->delete();
        }

        if ($touch) {
            $this->touchIfTouching();
        }

        return $results;
    }
608493
public function detach($ids = null, $touch = true)
    {
        if (is_null($ids)) {
            $ids = $this->query->pluck($this->query->qualifyColumn($this->relatedKey))->toArray();
        }

        list($idsOnly) = $this->getIdsWithAttributes($ids);

        $this->parent->fireModelEvent('pivotDetaching', true, $this->getRelationName(), $idsOnly);
        $parentResult = parent::detach($ids, $touch);
        $this->parent->fireModelEvent('pivotDetached', false, $this->getRelationName(), $idsOnly);

        return $parentResult;
    }
856217
public function deleteRecord($id)
    {
        if (is_object($id)) {
            $entity = $id;
        } else {
            $entity = $this->find($id);
        }

        $this->checkIfOriginRecord($entity);
        $return = $entity;

        if (is_callable([$entity, 'getId']) && $entity->getId() != null) {
            $this->deleteById($entity->getId());
            // detach so odm knows it's gone
            $this->manager->detach($entity);
            $this->manager->clear();
            // Dispatch ModelEvent
            $this->dispatchModelEvent(ModelEvent::MODEL_EVENT_DELETE, $return);
            $return = null;
        }

        return $return;
    }
1343722
protected function postHandler($className, $id, $relation)
    {
        if ($id) {
            if (!$relation) {
                $this->response->setStatusCode(409);
                return 'Conflict';
            }

            $obj = DataObject::get_by_id($className, $id);
            if (!$obj) {
                return $this->notFound();
            }

            $reqFormatter = $this->getRequestDataFormatter($className);
            if (!$reqFormatter) {
                return $this->unsupportedMediaType();
            }

            $relation = $reqFormatter->getRealFieldName($className, $relation);

            if (!$obj->hasMethod($relation)) {
                return $this->notFound();
            }

            if (!Config::inst()->get($className, 'allowed_actions') ||
                !in_array($relation, Config::inst()->get($className, 'allowed_actions'))) {
                return $this->permissionFailure();
            }

            $obj->$relation();

            $this->getResponse()->setStatusCode(204); // No Content
            return true;
        }

        if (!singleton($className)->canCreate($this->getMember())) {
            return $this->permissionFailure();
        }

        $obj = Injector::inst()->create($className);

        $reqFormatter = $this->getRequestDataFormatter($className);
        if (!$reqFormatter) {
            return $this->unsupportedMediaType();
        }

        $responseFormatter = $this->getResponseDataFormatter($className);

        try {
            /** @var DataObject|string $obj */
            $obj = $this->updateDataObject($obj, $reqFormatter);
        } catch (ValidationException $e) {
            return $this->validationFailure($responseFormatter, $e->getResult());
        }

        if (is_string($obj)) {
            return $obj;
        }

        $this->getResponse()->setStatusCode(201); // Created
        $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());

        // Append the default extension for the output format to the Location header
        // or else we'll use the default (XML)
        $types = $responseFormatter->supportedExtensions();
        $type = '';
        if (count($types)) {
            $type = ".{$types[0]}";
        }

        // Deviate slightly from the spec: Helps datamodel API access restrict
        // to consulting just canCreate(), not canView() as a result of the additional
        // "Location" header.
        if ($this->config()->get('location_header_on_create')) {
            $urlSafeClassName = $this->sanitiseClassName(get_class($obj));
            $apiBase = $this->config()->api_base;
            $objHref = Director::absoluteURL($apiBase . "$urlSafeClassName/$obj->ID" . $type);
            $this->getResponse()->addHeader('Location', $objHref);
        }

        return $responseFormatter->convertDataObject($obj);
    }
550883
public function detach( $name, $id )
    {
        if ( !isset( $this->listeners[$name][$id] ) )
        {
            return false;
        }

        unset( $this->listeners[$name][$id] );
        return true;
    }
1370454
public static function delete($id, $additionalAttributes = null)
    {
        static::invalidateCache($id);

        return \Phramework\Database\Operations\Delete::delete(
            $id,
            (
                $additionalAttributes !== null
                ? (array) $additionalAttributes
                : []
            ),
            (
                static::getSchema() === null
                ? static::getTable()
                : [
                    'table' => static::getTable(),
                    'schema' => static::getSchema()
                ]
            ),
            static::getIdAttribute()
        );
    }
366651
public static function buildDeleteRelationQuery(Relation $relation) {
        $query = "DELETE FROM `" . $relation->getTable() . "` ";
        $query .= "WHERE " . $relation->getLocalColumn() . " = :id;";
        return $query;
    }
1066183
public function detach($id = array(), $touch = true) {
        if (!$id instanceof Model and ! $id instanceof Collection) {
            $id = $this->modelsFromIds($id);
        } elseif (!is_array($id)) {
            $id = [$id];
        }

        // Prepare for a batch operation to take place so that we don't
        // overwhelm the database with many delete hits.
        $this->finder->prepareBatch();

        foreach ($id as $model) {
            $edge = $this->edge($model);
            $edge->delete();
        }

        $results = $this->finder->commitBatch();

        if ($touch)
            $this->touchIfTouching();

        return $results;
    }
106795
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model)
        {
            // In the case of fetching nested relations, we will get an array
            // with the first key being the model we need, and the other being
            // the related model so we'll just take the first model out of the array.
            if (is_array($model)) $model = reset($model);

            $model->setRelation($relation, $this->related->newCollection());
        }

        return $models;
    }
269726
public function initialize($attributes, $id = null)
    {
        if ($this->started) {
            return $this;
        }
        
        $this->id = $id ?: $this->id;
        $this->attributes = $attributes;
        $this->started = true;
        
        return $this;
    }
355714
protected function deleteModels($attribute)
    {
        $data = $this->relationalData[$attribute];

        /** @var ActiveRecord $model */
        foreach ($data['oldModels'] as $model) {
            if ($this->isDeletedModel($model, $attribute)) {
                if (!$model->delete()) {
                    Yii::$app->getDb()->getTransaction()->rollBack();
                    throw new RelationException('Model ' . $model::className() . ' not deleted due to unknown error');
                }
            }
        }

        $this->relationsMap($data['oldRows'], function($row) use ($attribute, $data) {
            if ($this->isDeletedRow($row, $attribute)) {
                Yii::$app->db->createCommand()
                    ->delete($data['junctionTable'], $row)
                    ->execute();
            }
        });
    }
984650
function removeRelatedObjectItem( $contentObjectAttribute, $objectID )
    {
        $xmlText = $contentObjectAttribute->attribute( 'data_text' );
        if ( trim( $xmlText ) == '' ) return;

        $doc = $this->parseXML( $xmlText );

        $return = false;
        $root = $doc->documentElement;
        $relationList = $root->getElementsByTagName( 'relation-list' )->item( 0 );
        if ( $relationList )
        {
            $relationItems = $relationList->getElementsByTagName( 'relation-item' );
            if ( !empty( $relationItems ) )
            {
                foreach( $relationItems as $relationItem )
                {
                    if ( $relationItem->getAttribute( 'contentobject-id' ) == $objectID )
                    {
                        $relationList->removeChild( $relationItem );
                        $return = true;
                    }
                }
            }
        }
        $this->storeObjectDOMDocument( $doc, $contentObjectAttribute );
        return $return;
    }
604134
public function updateLink($id, array $attributes): bool
	{
		$success = true;

		$foreignKey = $this->getForeignKey();

		$foreignKeyValue = $this->parent->getPrimaryKeyValue();

		$junctionKey = $this->getJunctionKey();

		foreach($this->getJunctionKeys($id) as $key => $id)
		{
			$success = $success && (bool) $this->junction()->where($foreignKey, '=', $foreignKeyValue)->where($junctionKey, '=', $id)->update($this->getJunctionAttributes($key, $attributes));
		}

		return $success;
	}