0 ? array_shift($temp_args) : 'all'; $options = count($temp_args) > 0 ? array_shift($temp_args) : array(); return static::deleted($find_type, $options); } return parent::__callStatic($method, $args); } protected function delete_self() { // If soft deleting has been disabled then just call the parent's delete if ($this->_disable_soft_delete) { return parent::delete_self(); } $deleted_column = static::soft_delete_property('deleted_field', static::$_default_field_name); $mysql_timestamp = static::soft_delete_property('mysql_timestamp', static::$_default_mysql_timestamp); // Generate the correct timestamp and save it $this->{$deleted_column} = $mysql_timestamp ? \Date::forge()->format('mysql') : \Date::forge()->get_timestamp(); $result = $this->save(false); return $result; } /** * Permanently deletes records using the parent Model delete function * * @param $cascade boolean * @param $use_transaction boolean * * @return boolean */ public function purge($cascade = null, $use_transaction = false) { $this->_disable_soft_delete = true; $result = parent::delete($cascade, $use_transaction); $this->_disable_soft_delete = false; return $result; } /** * Returns true unless the related model is not soft or temporal */ protected function should_cascade_delete($rel) { // Because temporal includes soft delete functionality it can be deleted too if ( ! is_subclass_of($rel->model_to, 'Orm\Model_Soft') && ! is_subclass_of($rel->model_to, 'Orm\Model_Temporal')) { // Throw if other is not soft throw new RelationNotSoft('Both sides of the relation must be subclasses of Model_Soft or Model_Temporal if cascade delete is true. '.$rel->model_to.' was found instead.'); } return true; } /** * Allows a soft deleted entry to be restored. */ public function restore($cascade_restore = null) { $deleted_column = static::soft_delete_property('deleted_field', static::$_default_field_name); $this->{$deleted_column} = null; //Loop through all relations and delete if we are cascading. $this->freeze(); foreach ($this->relations() as $rel_name => $rel) { //get the cascade delete status $rel_cascade = is_null($cascade_restore) ? $rel->cascade_delete : (bool) $cascade_restore; //Make sure that the other model is soft delete too if ($rel_cascade) { if (! is_subclass_of($rel->model_to, 'Orm\Model_Soft')) { //Throw if other is not soft throw new RelationNotSoft('Both sides of the relation must be subclasses of Model_Soft if cascade delete is true'); } if (get_class($rel) != 'Orm\ManyMany') { $model_to = $rel->model_to; $model_to::disable_filter(); //Loop through and call restore on all the models foreach ($rel->get($this) as $model) { $model->restore($cascade_restore); } $model_to::enable_filter(); } } } $this->unfreeze(); return $this->save(); } /** * Alias of restore() */ public function undelete() { return $this->restore(); } /** * Overrides the query method to allow soft delete items to be filtered out. */ public static function query($options = array()) { $query = Query_Soft::forge(get_called_class(), static::connection(), $options); if (static::get_filter_status()) { //Make sure we are filtering out soft deleted items $query->set_soft_filter(static::soft_delete_property('deleted_field', static::$_default_field_name)); } return $query; } /** * Alisas of find() but selects only deleted entries rather than non-deleted * ones. */ public static function deleted($id = null, array $options = array()) { //Make sure we are not filtering out soft deleted items $deleted_column = static::soft_delete_property('deleted_field', static::$_default_field_name); $options['where'][] = array($deleted_column, 'IS NOT', null); static::disable_filter(); $result = parent::find($id, $options); static::enable_filter(); return $result; } }