CI_DB_active_record::delete PHP Method

delete() public method

Compiles a delete string and runs the query
public delete ( $table = '', $where = '', $limit = NULL, $reset_data = TRUE ) : object
return object
    function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
    {
        // Combine any cached components with the current statements
        $this->_merge_cache();
        if ($table == '') {
            if (!isset($this->ar_from[0])) {
                if ($this->db_debug) {
                    return $this->display_error('db_must_set_table');
                }
                return FALSE;
            }
            $table = $this->ar_from[0];
        } elseif (is_array($table)) {
            foreach ($table as $single_table) {
                $this->delete($single_table, $where, $limit, FALSE);
            }
            $this->_reset_write();
            return;
        } else {
            $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
        }
        if ($where != '') {
            $this->where($where);
        }
        if ($limit != NULL) {
            $this->limit($limit);
        }
        if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0) {
            if ($this->db_debug) {
                return $this->display_error('db_del_must_use_where');
            }
            return FALSE;
        }
        $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
        if ($reset_data) {
            $this->_reset_write();
        }
        return $this->query($sql);
    }

Usage Example

示例#1
0
 /**
  * Delete record(s) from this table, optionally extending by specifying a $Join to
  * delete adjoining data from both
  *
  * @param ifx_Model $JoinObject
  * @param mixed $JoinType
  *
  * @return bool
  */
 public final function delete(ifx_Model $JoinObject = null, $JoinType = 'INNER')
 {
     if (!empty($JoinObject) and is_a($JoinObject, get_class())) {
         //Generate the join
         $this->_forge_join($JoinObject, $JoinType);
         //Generate the query
         $SQL = 'DELETE ' . $JoinObject->_table() . ', ' . $this->_table() . ' FROM ' . $this->_forged_join();
         //If this object was loaded, limit by this record
         if ($this->is_loaded()) {
             $this->db->where($this->_table() . '.' . $this->_id(), $this->id());
         }
         //If the join object was loaded, limit the query by the loaded record
         if ($JoinObject->is_loaded()) {
             $this->db->where($JoinObject->_table() . '.' . $JoinObject->_id(), $JoinObject->id());
         }
         //Get the WHERE portion from CI_DB_AR
         $Where = implode(' ', $this->db->ar_where);
         $this->db->ar_where = array();
         //Join the WHERE, if any
         $SQL .= !empty($Where) ? ' WHERE ' . $Where : '';
         //Run the query
         $this->db->query($SQL);
         //Reset the forged table for next time
         $this->_clear_forged_join();
     } elseif ($this->is_loaded()) {
         //We only allow deletes for a loaded record, to avoid whole table deletes
         $this->db->where($this->_id(), $this->id());
         $this->db->delete($this->_table());
     }
     if ($this->db->affected_rows() >= 1) {
         $this->_data = array();
         return true;
     }
     return false;
 }
All Usage Examples Of CI_DB_active_record::delete