final public function destroy($record = null, string $alias = null): Record
    {
        $record = Record::parse($record);

        foreach ($this->getParents() as $parent) {
            /** @var DataMapper $parent */
            $record->import($parent->destroy($record)->all());
        }

        $action = coalesce($alias, Action::DESTROY);

        $previous = $this->previous($record, $this->hashKey);

        if ($previous->isEmpty()) {
            throw new SimplesResourceError([$this->getHashKey() => $record->get($this->getHashKey())]);
        }

        if (!$this->before($action, $record, $previous)) {
            throw new SimplesHookError(get_class($this), $action, 'before');
        }

        $filter = new Filter($this->get($this->getPrimaryKey()), $record->get($this->getPrimaryKey()));
        $filters = [$filter];

        if ($this->destroyKeys) {
            $fields = [];
            $values = [];
            foreach ($this->destroyKeys as $type => $deletedKey) {
                $fields[] = $deletedKey;
                $values[] = $this->getTimestampValue($type);
            }

            $removed = $this
                ->source($this->getCollection())
                ->fields($fields)
                ->where($filters)// TODO: needs review
                ->change($values, [$filter->getValue()]);
        }

        if (!isset($removed)) {
            $removed = $this
                ->source($this->getCollection())
                ->where($filters)// TODO: needs review
                ->remove([$record->get($this->getPrimaryKey())]);
        }

        $this->reset();

        if (!$removed) {
            throw new SimplesActionError(get_class($this), $action);
        }
        $record = $previous->merge($record->all());

        if (!$this->after($action, $record)) {
            throw new SimplesHookError(get_class($this), $action, 'after');
        }
        return $record;
    }
final public function destroy($record = null, string $alias = null): Record
    {
        $record = Record::parse($record);

        foreach ($this->getParents() as $parent) {
            /** @var DataMapper $parent */
            $record->import($parent->destroy($record)->all());
        }

        $action = coalesce($alias, Action::DESTROY);

        $previous = $this->previous($record, $this->hashKey);

        if ($previous->isEmpty()) {
            throw new SimplesResourceError([$this->getHashKey() => $record->get($this->getHashKey())]);
        }

        if (!$this->before($action, $record, $previous)) {
            throw new SimplesHookError(get_class($this), $action, 'before');
        }

        $filter = new Filter($this->get($this->getPrimaryKey()), $record->get($this->getPrimaryKey()));
        $filters = [$filter];

        if ($this->destroyKeys) {
            $fields = [];
            $values = [];
            foreach ($this->destroyKeys as $type => $deletedKey) {
                $fields[] = $deletedKey;
                $values[] = $this->getTimestampValue($type);
            }

            $removed = $this
                ->source($this->getCollection())
                ->fields($fields)
                ->where($filters)// TODO: needs review
                ->change($values, [$filter->getValue()]);
        }

        if (!isset($removed)) {
            $removed = $this
                ->source($this->getCollection())
                ->where($filters)// TODO: needs review
                ->remove([$record->get($this->getPrimaryKey())]);
        }

        $this->reset();

        if (!$removed) {
            throw new SimplesActionError(get_class($this), $action);
        }
        $record = $previous->merge($record->all());

        if (!$this->after($action, $record)) {
            throw new SimplesHookError(get_class($this), $action, 'after');
        }
        return $record;
    }
final public function destroy ( $ record = null , string $ alias = null ) : Record { $ record = Record :: parse ( $ record ) ; foreach ( $ this -> getParents ( ) as $ parent ) { /** @var DataMapper $parent */ $ record -> import ( $ parent -> destroy ( $ record ) -> all ( ) ) ; } $ action = coalesce ( $ alias , Action :: DESTROY ) ; $ previous = $ this -> previous ( $ record , $ this -> hashKey ) ; if ( $ previous -> isEmpty ( ) ) { throw new SimplesResourceError ( [ $ this -> getHashKey ( ) => $ record -> get ( $ this -> getHashKey ( ) ) ] ) ; } if ( ! $ this -> before ( $ action , $ record , $ previous ) ) { throw new SimplesHookError ( get_class ( $ this ) , $ action , 'before' ) ; } $ filter = new Filter ( $ this -> get ( $ this -> getPrimaryKey ( ) ) , $ record -> get ( $ this -> getPrimaryKey ( ) ) ) ; $ filters = [ $ filter ] ; if ( $ this -> destroyKeys ) { $ fields = [ ] ; $ values = [ ] ; foreach ( $ this -> destroyKeys as $ type => $ deletedKey ) { $ fields [ ] = $ deletedKey ; $ values [ ] = $ this -> getTimestampValue ( $ type ) ; } $ removed = $ this -> source ( $ this -> getCollection ( ) ) -> fields ( $ fields ) -> where ( $ filters ) // TODO: needs review -> change ( $ values , [ $ filter -> getValue ( ) ] ) ; } if ( ! isset ( $ removed ) ) { $ removed = $ this -> source ( $ this -> getCollection ( ) ) -> where ( $ filters ) // TODO: needs review -> remove ( [ $ record -> get ( $ this -> getPrimaryKey ( ) ) ] ) ; } $ this -> reset ( ) ; if ( ! $ removed ) { throw new SimplesActionError ( get_class ( $ this ) , $ action ) ; } $ record = $ previous -> merge ( $ record -> all ( ) ) ; if ( ! $ this -> after ( $ action , $ record ) ) { throw new SimplesHookError ( get_class ( $ this ) , $ action , 'after' ) ; } return $ record ; }
Remove the given record of database
@param array|Record $record (null)
@param string $alias ('destroy')
@return Record
@throws SimplesActionError
@throws SimplesHookError
@throws SimplesResourceError
@throws SimplesRunTimeError
Remove the given record of database