PodsData::delete PHP Method

delete() public method

Delete an item
Since: 2.0
public delete ( string $table, array $where, array $where_format = null ) : array | boolean | mixed | null | void
$table string Table name
$where array A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw".
$where_format array (optional) An array of formats to be mapped to each of the values in $where.
return array | boolean | mixed | null | void
    public function delete($table, $where, $where_format = null)
    {
        /**
         * @var $wpdb wpdb
         */
        global $wpdb;
        if (strlen($table) < 1 || empty($where) || !is_array($where)) {
            return false;
        }
        $wheres = array();
        $where_formats = $where_format = (array) $where_format;
        foreach ((array) array_keys($where) as $field) {
            if (!empty($where_format)) {
                $form = ($form = array_shift($where_formats)) ? $form : $where_format[0];
            } elseif (isset(self::$field_types[$field])) {
                $form = self::$field_types[$field];
            } elseif (isset($wpdb->field_types[$field])) {
                $form = $wpdb->field_types[$field];
            } else {
                $form = '%s';
            }
            $wheres[] = "`{$field}` = {$form}";
        }
        $sql = "DELETE FROM `{$table}` WHERE " . implode(' AND ', $wheres);
        list($sql, $where) = $this->do_hook('delete', array($sql, array_values($where)), $table, $where, $where_format, $wheres);
        return $this->query(self::prepare($sql, $where));
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param null $id
  *
  * @return bool|mixed
  */
 public function delete_bulk()
 {
     $this->do_hook('pre_delete_bulk');
     if (1 != pods_var('deleted_bulk', 'get', 0)) {
         $ids = $this->bulk;
         $success = false;
         if (!empty($ids)) {
             $ids = (array) $ids;
             foreach ($ids as $id) {
                 $id = pods_absint($id);
                 if (empty($id)) {
                     continue;
                 }
                 if (isset($this->actions_custom['delete']) && is_callable($this->actions_custom['delete'])) {
                     $check = call_user_func_array($this->actions_custom['delete'], array($id, &$this));
                     if (false !== $check) {
                         $check = true;
                     }
                 } elseif (is_object($this->pod)) {
                     $check = $this->pod->delete($id);
                 } else {
                     $check = $this->pods_data->delete($this->table, array($this->data->field_id => $id));
                 }
                 if ($check) {
                     $success = true;
                 }
             }
         }
         if ($success) {
             pods_redirect(pods_var_update(array('action_bulk' => 'delete', 'deleted_bulk' => 1), array('page', 'lang', 'action', 'id')));
         } else {
             $this->error(__("<strong>Error:</strong> {$this->item} has not been deleted.", 'pods'));
         }
     } else {
         $this->message(__("<strong>Deleted:</strong> {$this->items} have been deleted.", 'pods'));
         unset($_GET['deleted_bulk']);
     }
     $this->action_bulk = false;
     unset($_GET['action_bulk']);
     $this->do_hook('post_delete_bulk');
     $this->manage();
 }
All Usage Examples Of PodsData::delete