Contao\DC_Table::delete PHP Метод

delete() публичный Метод

Delete a record of the current table table and save it to tl_undo
public delete ( boolean $blnDoNotRedirect = false )
$blnDoNotRedirect boolean
    public function delete($blnDoNotRedirect = false)
    {
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['notDeletable']) {
            throw new InternalServerErrorException('Table "' . $this->strTable . '" is not deletable.');
        }
        if (!$this->intId) {
            $this->redirect($this->getReferer());
        }
        $delete = array();
        // Do not save records from tl_undo itself
        if ($this->strTable == 'tl_undo') {
            $this->Database->prepare("DELETE FROM " . $this->strTable . " WHERE id=?")->limit(1)->execute($this->intId);
            $this->redirect($this->getReferer());
        }
        // If there is a PID field but no parent table
        if ($this->Database->fieldExists('pid', $this->strTable) && !strlen($this->ptable)) {
            $delete[$this->strTable] = $this->Database->getChildRecords($this->intId, $this->strTable);
            array_unshift($delete[$this->strTable], $this->intId);
        } else {
            $delete[$this->strTable] = array($this->intId);
        }
        // Delete all child records if there is a child table
        if (!empty($this->ctable)) {
            foreach ($delete[$this->strTable] as $id) {
                $this->deleteChilds($this->strTable, $id, $delete);
            }
        }
        $affected = 0;
        $data = array();
        // Save each record of each table
        foreach ($delete as $table => $fields) {
            foreach ($fields as $k => $v) {
                $objSave = $this->Database->prepare("SELECT * FROM " . $table . " WHERE id=?")->limit(1)->execute($v);
                if ($objSave->numRows) {
                    $data[$table][$k] = $objSave->row();
                    // Store the active record
                    if ($table == $this->strTable && $v == $this->intId) {
                        $this->objActiveRecord = $objSave;
                    }
                }
                $affected++;
            }
        }
        $this->import('BackendUser', 'User');
        $objUndoStmt = $this->Database->prepare("INSERT INTO tl_undo (pid, tstamp, fromTable, query, affectedRows, data) VALUES (?, ?, ?, ?, ?, ?)")->execute($this->User->id, time(), $this->strTable, 'DELETE FROM ' . $this->strTable . ' WHERE id=' . $this->intId, $affected, serialize($data));
        // Delete the records
        if ($objUndoStmt->affectedRows) {
            $undoId = $objUndoStmt->insertId;
            // Call ondelete_callback
            if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['ondelete_callback'])) {
                foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['ondelete_callback'] as $callback) {
                    if (is_array($callback)) {
                        $this->import($callback[0]);
                        $this->{$callback[0]}->{$callback[1]}($this, $undoId);
                    } elseif (is_callable($callback)) {
                        $callback($this, $undoId);
                    }
                }
            }
            // Delete the records
            foreach ($delete as $table => $fields) {
                foreach ($fields as $v) {
                    $this->Database->prepare("DELETE FROM " . $table . " WHERE id=?")->limit(1)->execute($v);
                }
            }
            // Add a log entry unless we are deleting from tl_log itself
            if ($this->strTable != 'tl_log') {
                $this->log('DELETE FROM ' . $this->strTable . ' WHERE id=' . $data[$this->strTable][0]['id'], __METHOD__, TL_GENERAL);
            }
        }
        if (!$blnDoNotRedirect) {
            $this->redirect($this->getReferer());
        }
    }

Usage Example

 /**
  * @param bool $blnDoNotRedirect
  */
 public function delete($blnDoNotRedirect = false)
 {
     // Define ondelete callback for every deltion
     $GLOBALS['TL_DCA'][$this->strTable]['config']['ondelete_callback'][] = array('\\TranslationFields\\TranslationFieldsBackendHelper', 'deleteDataRecord');
     // Call parent
     parent::delete($blnDoNotRedirect);
 }