Custom data types and database table creation and CURD operations
Defining Data Structures
Adding new data structures involves adding items to the global variables $table and $datainfo arrays.
# In this article, we will create a class called Custom
# Table Name
$table['Custom'] = '%pre%plugin_custom';
# Note: The table name can be customized, but it must be prefixed with %pre% to distinguish tables generated by different programs within the same database.
# Table Structure
$datainfo['Custom'] = array(
'ID' => array('cu_ID', 'integer', '', 0),
'Content' => array('cu_Content', 'string', 250, ''),
'LogID' => array('cu_Logid', 'integer', '', 0)
);
# The first item in the $datainfo array must be 'ID', which is the table's unique auto-incrementing sequence.The data table structure, $datainfo, is declared as follows:
array('Attribute Name' => array('Field Name', 'Type', 'Length Parameter (defined by type)', 'Default Value'));
# Simple demonstration of common type declarations:
# int (number)
array('ID', 'integer', '', 0),
# boolean
array('Check', 'boolean', '', false),
# char
array('Value', 'char', 10, ''),
# String with a maximum length of 250
array('Title', 'string', 250, ''),
# Text string of unlimited length
array('Content', 'string', '', ''),Creating Data Types
Custom data types are created by defining a new class that inherits from the system's Base class.
# Here we define a basic Custom class. You can extend your own methods within this class.
class Custom extends Base
{
public function __construct()
{
global $zbp;
parent::__construct($zbp->table['Custom'], $zbp->datainfo['Custom'], __CLASS__);
}
}Creating Database Tables
To create a database table, you would do so within the Install function on your application's include.php page, checking for the table's existence and then creating it.
function InstallPlugin_YourAppID()
{
global $zbp;
# Check if the table has already been created; if not, create a new one.
if (!$zbp->db->ExistTable($zbp->table['Custom']))
{
$s = $zbp->db->sql->CreateTable($zbp->table['Custom'], $zbp->datainfo['Custom']);
$zbp->db->QueryMulit($s);
}
}
# PS: When uninstalling the application, you can choose to delete the created data tables or retain them based on your needs.Deleting Database Tables
The method for deleting tables. When exactly to delete them is up to you.
function YourPluginID_delTable()
{
global $zbp;
if ($zbp->db->ExistTable($zbp->table['Custom'])) {
$s = $zbp->db->sql->DelTable($zbp->table['Custom']);
$zbp->db->QueryMulit($s);
}
}
# PS: When uninstalling the application, you can choose to delete the created data tables or retain them based on your needs.CRUD Operations for Custom Types
Adding a new record.
$c = new Custom(); $c->Content = 'abc'; $c->LogID = 123; $c->Save(); echo $c->ID; // Outputs the ID value of the newly inserted object.
Updating a record.
$c = new Custom();
if ($c->LoadInfoByID(1) == true) {
$c->Content = '12345';
$c->Save();
}Deleting a record.
$c = new Custom();
if ($c->LoadInfoByID(2) == true) {
$c->Del();
}Querying a single record.
$c = new Custom(); $c->LoadInfoByID(123); # LoadInfoByID returns false if the load is unsuccessful.
Querying multiple records.
# Query all records in the Custom table that meet two conditions.
$w[] = array('=', $zbp->d['Custom']['LogID'][0], 123); // Condition 1: LogID value is 123
$w[] = array('=', $zbp->d['Custom']['Content'][0], 'abc'); // Condition 2: Content value is 'abc'
$sql = $zbp->db->sql->Select($zbp->t['Custom'], array('*'), $w);
$list = $zbp->GetListType('Custom', $sql);
# The result $list is an array containing Custom objects.New Query Methods Added in Main Program Version 1.7.2
Main Program Version 1.7.2 automatically generated 3 methods in the ZBlogPHP class for reading and loading the Custom class after its successful definition. These are:
GetCustomList($select = null, $where = null, $order = null, $limit = null, $option = null)# Query all records in the Custom table that meet two conditions. # Using zbp's chained SQL operation component this time. $sql = $zbp->db->sql->get() ->select($zbp->table['Custom']) ->where(array('=', 'cu_Logid', '123')) // Condition 1: LogID is 123 ->where(array('=', 'cu_Content', 'abc')) // Condition 2: Content value is 'abc' ->orderBy(array('cu_ID' => 'desc')) // Order by ID in descending order ->sql; $list = $zbp->GetCustomList($sql); # If no data is retrieved, $list will return an empty array().GetCustomByID($id)# Read a single Custom instance from the database. $c = $zbp->GetCustomByID(111); # Returns the instance on successful read, otherwise returns null.
GetCustomByArray($array, $field_name = 'ID')# Query Custom instances from the database whose ID values match the items in the given array. # The $field_name parameter can be omitted if querying by ID. $list = $zbp->GetCustomByArray(array(1, 3, 5, 666)); # Returns an array of Custom instances (with IDs 1, 3, 5, 666, if they exist).
Introducing a hidden, complex usage of GetCustomByArray.
# Suppose there is a posts array containing 3 post instances with IDs 1, 2, and 13.
$posts = array(
$zbp->GetPostByID(1),
$zbp->GetPostByID(2),
$zbp->GetPostByID(13),
);
# Query Custom instances from the Custom data table where the LogID value matches the ID value of the post instances in $posts.
$list = $zbp->GetCustomByArray($posts, array('ID' => 'LogID'));
# Returns an array of Custom instances, where their LogID values are all 1, 2, or 13.The code used in this article requires Main Program Version 1.7 or later to function.
Article link: https://docs.zbp.cool/zblogdocs/zblogapp/15.html
Helpful?
2025-10-09 21:14:08