Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WPDB sample
<?php
namespace SolusiPress\Model;
class Base {
public $db = null;
public $table = null;
protected $sqlOrder = '';
protected $sqlWhere = '';
protected $filtered = array();
protected $data_filtered = false;
public function __construct() {
global $wpdb;
$this->db = $wpdb;
}
public function get_row( $post_id ) {
$sql = $this->db->prepare( "select * from {$this->table} where post_id=%d", $post_id );
$row = $this->db->get_row( $sql );
return $row;
}
public function get_by( $field, $value, $format='%d' ) {
$sql = $this->db->prepare( "select * from {$this->table} where {$field}={$format} and date_trashed IS NULL", $value );
$row = $this->db->get_row( $sql );
return $row;
}
public function get_all_by( $field, $value, $format='%d' ) {
$sql = $this->db->prepare( "select * from {$this->table} where {$field}={$format} and date_trashed IS NULL", $value );
$rows = $this->db->get_results( $sql );
return $rows;
}
public function trash( $post_id ) {
$check = $this->get_row( $post_id );
if( $check ) {
$data = array( 'date_trashed' => current_time( 'mysql' ) );
$this->db->update( $this->table, $data, array( 'id' => $check->id ), array( '%s' ), array( '%d' ) );
}
}
public function untrash( $post_id ) {
$check = $this->get_row( $post_id );
if( $check ) {
$data = array( 'date_trashed' => null );
$this->db->update( $this->table, $data, array( 'id' => $check->id ), array( '%s' ), array( '%d' ) );
}
}
}
<?php
namespace SolusiPress\Model;
use SolusiPress\Model\Base as BaseModel;
class Employee extends BaseModel {
private static $_instance = null;
private $post_id = null;
private function __construct() {
parent::__construct();
$this->table = $this->db->prefix . 'app_employees';
}
private function __clone() { }
public static function getInstance() {
if( !is_object(self::$_instance) )
self::$_instance = new self();
return self::$_instance;
}
private function get_meta( $key ) {
$value = get_post_meta( $this->post_id, $key, true );
if( !$value ) $value = null;
if( $key == 'empfunction_id' ) {
$sql = $this->db->prepare( "select * from " . $this->db->prefix.'app_empfunctions' . " where post_id=%d", $value );
$row = $this->db->get_row( $sql );
if( $row ) {
$value = $row->id;
} else {
$value = null;
}
}
return $value;
}
public function insertOrUpdate( $post_id ) {
$this->post_id = $post_id;
$check = $this->db->get_row( $this->db->prepare( "select * from {$this->table} where post_id=%d", $post_id ) );
$data = [
'post_id' => $post_id,
'emp_number' => $this->get_meta( 'emp_number' ),
'full_name' => $this->get_meta( 'full_name' ),
'gender' => $this->get_meta( 'gender' ),
'birth_date' => $this->get_meta( 'birth_date' ),
'email' => $this->get_meta( 'email' ),
'mobile' => $this->get_meta( 'mobile' ),
'join_date' => $this->get_meta( 'join_date' ),
'empfunction_id' => $this->get_meta( 'empfunction_id' ),
'empfunction_date' => $this->get_meta( 'empfunction_date' ),
];
$formats = [ '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s' ];
if( $check ) {
$this->db->update( $this->table, $data, array( 'id' => $check->id ), $formats, array( '%d' ) );
} else {
$this->db->insert( $this->table, $data, $formats );
}
wp_update_post( array(
'ID' => $post_id,
'post_title' => $data['full_name']
) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment