LazyRecord\BaseModel::delete PHP Method

delete() public method

Delete current record, the record should be loaded already.
public delete ( ) : Result
return Result operation result (success or error)
    public function delete()
    {
        $k = static::PRIMARY_KEY;
        if (!isset($this->_data[$k])) {
            throw new Exception('Record is not loaded, Record delete failed.');
        }
        $kVal = $this->_data[$k];
        if (!$this->currentUserCan($this->getCurrentUser(), 'delete')) {
            return $this->reportError(_('Permission denied. Can not delete record.'), array());
        }
        $dsId = $this->writeSourceId;
        $conn = $this->getWriteConnection();
        $driver = $this->getWriteQueryDriver();
        $this->beforeDelete($this->_data);
        $arguments = new ArgumentArray();
        $query = new DeleteQuery();
        $query->delete($this->table);
        $query->where()->equal($k, $kVal);
        $sql = $query->toSql($driver, $arguments);
        $vars = $arguments->toArray();
        $validationResults = array();
        $stm = $conn->prepare($sql);
        $stm->execute($arguments->toArray());
        $this->afterDelete($this->_data);
        $this->clear();
        return $this->reportSuccess('Record deleted', array('sql' => $sql, 'type' => Result::TYPE_DELETE));
    }

Usage Example

Example #1
0
 public function successfulDelete(BaseModel $record)
 {
     $this->assertResultSuccess($record->delete());
 }