Download the PHP package claytonkreisel/wpdb-tools without Composer
On this page you can find all versions of the php package claytonkreisel/wpdb-tools. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download claytonkreisel/wpdb-tools
More information about claytonkreisel/wpdb-tools
Files in claytonkreisel/wpdb-tools
Package wpdb-tools
Short Description This package provides tools to be used in custom WordPress themes or plugins that aid you in using the WPDB especially in the creation of custom tables
License MIT
Homepage https://github.com/claytonkreisel/wpdb-tools
Informations about the package wpdb-tools
WordPress Database Table Tools
A package to aid in WordPress theme and plugin development by adding classes that quickly allow you to add and manage custom tables to the WordPress database. This uses native WordPress API functions and makes it easy to setup model based I/O using abstract parent classes.
PHP Version Requirements
Version 1 of this library should work on PHP 7.4 or newer.
How to Install
Composer Install
or
Manual Install
Download the package and manually place it in your application then simply include the file like this at the beginning of your application:
How to Use
Custom Tables
This library contains a parent abstract class of . This class allows you to create a child class that will manage your database columns using an array defined in a child method and provides you with methods to , , and rows within that table.
Creating a Custom Table Class
In order to create a custom table you will create a child class that extends the class within this package.
NOTE: If you wish to create a table that also has a relational "metadata" table much like the and structure native to WordPress then please refer to the class. This class will simply create a single table without a corresponding meta table. That class will create and manage both the main table and the metadata table without the need of defining a class.
To create a Table
you will put the following code in your functions.php
file or another file that is included in your plugin or theme. This assumes you have already installed or included the classes through composer or manually.
After you have written this code you can now create a new instance of this class. Upon construction this class will fire a method that checks to see if the version of the database has changed. If it has then a database altering and cleanup will occur.
NOTE: Columns that are removed from the columns array method will remain in the database until you explicitly remove them using the method. This is to protect from accidental deletion of data.
Inserting a Row in a Custom Table
In order to insert a new row into a custom table you simply call the method on the object you initiated.
Inserting Multiple Rows in a Custom Table
In order to insert multiple rows into a custom table you simply call the method on the object you initiated with multiple associative arrays of data as the parameter.
NOTE: In order for this method to work properly the same keys must be passed in each iteration of the associative array.
Selecting Rows from Custom Tables
There are multiple ways to select data from your custom table. For more complex selections you can write your own statement or use an array driven method in order to make your syntax cleaner. Either way you will only need to use one of three methods in order to select data; , select
or select_or
.
Using the , and Methods
The method gives you the most versatility in your data selection approach. This method takes 3 parameters at present: , and .
The simply returns all the rows in the table. This method only takes the last 2 parameters at present: and .
- (array|string) - [Required]
- Can be an associative array, an array of arrays (view example for more definition), or a string in SQL syntax.
- NOTE: Not present in the method.
-
*(array)$return_typeARRAY_AARRAY_NOBJECTOBJECT_Kdelete$dataWHEREupdate$dataSET$whereWHERE$data$where$dataupdatecolumnsTabledbDeltareturncolumnsremove_columnremove_columncolumnsremove_columnremove_column$column_nameDROPDROP``` command on the column. You will lose all data in that column. You must either backup or migrate the data to another source if you wish to retain that information. Once it is dropped it is gone for good!
Below is an example on how to use this method: