Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A PHP class to handle CRUD functionality in WordPress default tables.
<?php
/**
* Abstract class which has helper functions to get data from the database
*/
abstract class Base_Custom_Data
{
/**
* The current table name
*
* @var boolean
*/
private $tableName = false;
/**
* Constructor for the database class to inject the table name
*
* @param String $tableName - The current table name
*/
public function __construct($tableName)
{
$this->tableName = $tableName;
}
/**
* Insert data into the current data
*
* @param array $data - Data to enter into the database table
*
* @return InsertQuery Object
*/
public function insert(array $data)
{
global $wpdb;
if(empty($data))
{
return false;
}
$wpdb->insert($this->tableName, $data);
return $wpdb->insert_id;
}
/**
* Get all from the selected table
*
* @param String $orderBy - Order by column name
*
* @return Table result
*/
public function get_all( $orderBy = NULL )
{
global $wpdb;
$sql = 'SELECT * FROM `'.$this->tableName.'`';
if(!empty($orderBy))
{
$sql .= ' ORDER BY ' . $orderBy;
}
$all = $wpdb->get_results($sql);
return $all;
}
/**
* Get a value by a condition
*
* @param Array $conditionValue - A key value pair of the conditions you want to search on
* @param String $condition - A string value for the condition of the query default to equals
*
* @return Table result
*/
public function get_by(array $conditionValue, $condition = '=')
{
global $wpdb;
$sql = 'SELECT * FROM `'.$this->tableName.'` WHERE ';
foreach ($conditionValue as $field => $value) {
switch(strtolower($condition))
{
case 'in':
if(!is_array($value))
{
throw new Exception("Values for IN query must be an array.", 1);
}
$sql .= $wpdb->prepare('`%s` IN (%s)', $field, implode(',', $value));
break;
default:
$sql .= $wpdb->prepare('`'.$field.'` '.$condition.' %s', $value);
break;
}
}
$result = $wpdb->get_results($sql);
return $result;
}
/**
* Update a table record in the database
*
* @param array $data - Array of data to be updated
* @param array $conditionValue - Key value pair for the where clause of the query
*
* @return Updated object
*/
public function update(array $data, array $conditionValue)
{
global $wpdb;
if(empty($data))
{
return false;
}
$updated = $wpdb->update( $this->tableName, $data, $conditionValue);
return $updated;
}
/**
* Delete row on the database table
*
* @param array $conditionValue - Key value pair for the where clause of the query
*
* @return Int - Num rows deleted
*/
public function delete(array $conditionValue)
{
global $wpdb;
$deleted = $wpdb->delete( $this->tableName, $conditionValue );
return $deleted;
}
}
?>
@Colinsp
Copy link

Colinsp commented Feb 26, 2014

Can you give some hints on how to use this please as it looks like exactly what I want?

@kct1989
Copy link

kct1989 commented Jul 22, 2014

oh nice

@ouidadadil
Copy link

ouidadadil commented Nov 18, 2014

/**
* Get some elements from the selected table
*
* @param array $data - Used restrict results
* @param String $orderBy - Order by column name
*
* @return Table result
*/
public function get_some( array $conditionValue, $orderBy = NULL )
{
global $wpdb;

    $sql = 'SELECT * FROM `'.$this->tableName.'`';

    if(isset($conditionValue)){
      $sql .= ' WHERE ';
      while (list($columns, $value) = each($conditionValue)) {
        $sql .= $columns . ' = \'' . $value . '\' AND ';
      }

      $sql = substr($sql, 0, -4);
    }

    if(!empty($orderBy))
    {
        $sql .= ' ORDER BY ' . $orderBy;
    }

    $all = $wpdb->get_results($sql);

    return $all;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment