PHP Class WPDKDBTableModel

If you would like use this model in a list table view controller, see the interface of WPDKListTableModel.
Author: =undo= ([email protected])
ファイルを表示 Open project: wpxtreme/wpdk Class Usage Examples

Public Properties

Property Type Description
$crud_results boolean Used for check the CRUD action results
$primary_key string Name of field as primary key
$sql_filename string The filename of SQL file with the database table structure and init data.
$table_name string The name of the database table with the WordPress prefix

Public Methods

Method Description
__construct ( string $table_name, string $sql_file = '', string $primary_key = 'id' ) : WPDKDBTableModel Create an instance of WPDKDBTableModel class
delete ( integer | array $id ) : integer | boolean Delete one or more record from table. Return the number of rows affected/selected or WP_Error.
group_by ( string $column, string $order = 'ASC' ) : mixed Return a list of groupped by column. Use 'value' as selector
insert ( ) : integer | WP_Error Insert a record by values and return the id on successfull, otherwise return WP_Error.
map ( object $source_row, object $destination_object ) : object | boolean Map the properties fields of single database row into a destination object model.
primaryKey ( ) : string Return the name of the primary key
select ( ) Select data
update ( ) : integer | WP_Error Update a record by values and retrun TRUE onsuccessfully, otherwise return a WP_Error.
update_table ( ) : boolean Do an update the table via WordPress dbDelta() function. Apply a new SQL file on the exists (or do not exists) table. Return TRUE on success
where ( array $args, string $key, string $table_prefix = '', array $not_in = [], string $cond = '=' ) : string Return a where condiction with possible OR values
where_date ( array $args, string $column_key, string $table_prefix = '', string $accuracy = '%Y-%m-%d %H:%i:%s' ) : string Return a where condiction for date and date time
where_filter ( array $args, string $filter, string $key, string $table_prefix = '', array $not_in = [], string $cond = '=' ) : string Return a where condiction with possible OR values for a filter. Useful for JOIN table

Method Details

__construct() public method

Create an instance of WPDKDBTableModel class
public __construct ( string $table_name, string $sql_file = '', string $primary_key = 'id' ) : WPDKDBTableModel
$table_name string The name of the database table without WordPress prefix
$sql_file string Optional. The filename of SQL file with the database table structure and init data.
$primary_key string Optional. If FALSE the primary key is get from database.
return WPDKDBTableModel

delete() public method

Use the primaryKey. The delete result is store in $this->crud_results.
public delete ( integer | array $id ) : integer | boolean
$id integer | array Any single int or array list of primary keys
return integer | boolean

group_by() public method

$results = ::group_by( 'column' ); foreach( $results as $row ) echo $row->value;
public group_by ( string $column, string $order = 'ASC' ) : mixed
$column string Column name
$order string Optional. Order 'ASC' or 'DESC'
return mixed

insert() public method

The insert result is store in $this->crud_results.
public insert ( ) : integer | WP_Error
return integer | WP_Error

map() public method

The $destination_object can implement a method named column_[property] to override map process.
public map ( object $source_row, object $destination_object ) : object | boolean
$source_row object Database record
$destination_object object Object to map
return object | boolean The destination object with new properties or FALSE if error.

primaryKey() public method

Return the name of the primary key
public primaryKey ( ) : string
return string

select() public method

Select data
public select ( )

update() public method

The update result is store in $this->crud_results.
public update ( ) : integer | WP_Error
return integer | WP_Error

update_table() public method

Do an update the table via WordPress dbDelta() function. Apply a new SQL file on the exists (or do not exists) table. Return TRUE on success
public update_table ( ) : boolean
return boolean

where() public static method

$where[] = ::where( $args, self::COLUMN_STATUS, '', array( WPXSSCouponStatus::ALL ) ); If $args[self::COLUMN_STATUS] is an array ( status = 'pending' OR status = 'confirmed' ) If $args[self::COLUMN_STATUS] is a string ( status = 'pending' ) $where[] = ::where( $args, self::COLUMN_STATUS, 'coupon', array( WPXSSCouponStatus::ALL ) ); If $args[self::COLUMN_STATUS] is a string ( coupon.status = 'pending' )
public static where ( array $args, string $key, string $table_prefix = '', array $not_in = [], string $cond = '=' ) : string
$args array Arguments list
$key string A key selector
$table_prefix string Optional. Table prefix
$not_in array Optional. Value to exclude
$cond string Optional. Condiction used, default '=' or 'LIKE'
return string

where_date() public static method

$where[] = ::where_date( $args, self::COLUMN_DATE ); If $args[self::COLUMN_DATE] is a string ( DATE_FORMAT( col_date, '%Y-%m-%d %H:%i:%s' ) = '2014-01-01' ) If $args[self::COLUMN_DATE] is an array( '2014-01-01', '2014-02-01' ) ( DATE_FORMAT( col_date, '%Y-%m-%d %H:%i:%s' ) >= '2014-01-01' ) AND DATE_FORMAT( col_date, '%Y-%m-%d %H:%i:%s' ) <= '2014-02-01' ) )
public static where_date ( array $args, string $column_key, string $table_prefix = '', string $accuracy = '%Y-%m-%d %H:%i:%s' ) : string
$args array Arguments list
$column_key string Name of column
$table_prefix string Optional. Table prefix
$accuracy string Optional. Default = '%Y-%m-%d %H:%i:%s'
return string

where_filter() public static method

$where[] = ::where( $args, self::FILTER_USER_ID, 'ID', 'users' ); ( users.ID = '34' )
public static where_filter ( array $args, string $filter, string $key, string $table_prefix = '', array $not_in = [], string $cond = '=' ) : string
$args array Arguments list
$filter string A key for filter
$key string A key selector
$table_prefix string Optional. Table prefix
$not_in array Optional. Value to exclude
$cond string Optional. Condiction used, default '=' or 'LIKE'
return string

Property Details

$crud_results public_oe property

Used for check the CRUD action results
public bool $crud_results
return boolean

$primary_key public_oe property

Name of field as primary key
public string $primary_key
return string

$sql_filename public_oe property

The filename of SQL file with the database table structure and init data.
public string $sql_filename
return string

$table_name public_oe property

The name of the database table with the WordPress prefix
public string $table_name
return string