lithium\data\Model::delete PHP Method

delete() public method

Deletes the data associated with the current Model.
public delete ( object $entity, array $options = [] ) : boolean
$entity object Entity to delete.
$options array Options.
return boolean Success.
    public function delete($entity, array $options = array())
    {
        $params = compact('entity', 'options');
        return static::_filter(__FUNCTION__, $params, function ($self, $params) {
            $options = $params + $params['options'] + array('model' => $self, 'type' => 'delete');
            unset($options['options']);
            $query = $self::invokeMethod('_instance', array('query', $options));
            return $self::connection()->delete($query, $options);
        });
    }

Usage Example

Exemplo n.º 1
0
 /**
  * overwritten to allow for soft-deleting a record
  *
  * The schema of the relevant model needs a field defined in schema called `deleted`.
  * As soon as this is the case, the record does not get deleted right away but instead
  * marked for deletion, i.e. setting a timestamp into the `deleted` field. Unless you make
  * use of the `force` option, then the record will get deleted without further ado.
  *
  * @param object $entity current instance
  * @param array $options Possible options are:
  *     - `force`: set to true to delete record, anyway
  * @return boolean true on success, false otherwise
  */
 public function delete($entity, array $options = array())
 {
     $options += array('force' => false);
     $deleted = $entity->schema('deleted');
     // TODO: use $deleted = $entity->hasField('deleted');
     if (is_null($deleted) || $options['force']) {
         unset($options['force']);
         $result = parent::delete($entity, $options);
         $versions = static::meta('versions');
         if ($versions === true || is_callable($versions) && $versions($entity, $options)) {
             if ($entity->version_id) {
                 $key = Versions::key();
                 $conditions = array($key => $entity->version_id);
                 Versions::update(array('status' => 'deleted'), $conditions);
             }
         }
         return $result;
     }
     $entity->deleted = time();
     return $entity->save();
 }