underpin / berlindb-extension
BerlinDB 在 Underpin 中的实现
1.3.0
2021-11-23 14:21 UTC
Requires
- berlindb/core: ^2.0
- underpin/underpin: ^2.0
This package is auto-updated.
Last update: 2024-09-23 22:19:42 UTC
README
BerlinDB 集成到 Underpin WordPress 框架。
一些关键优势
- 表通过
Database_Model
类进行注册。这个类提供了有用的上下文,使得与 BerlinDB 一起工作更加容易,同时帮助保持你的代码 DRY(Don't Repeat Yourself)。 - 所有表都存储在注册表中。这允许你一次性与所有表交互。需要卸载所有内容?你可以使用单个方法,而不是手动遍历表。
- 所有 CRUD 操作都通过数据库模型进行,而不是在各个地方创建
Query
实例。
安装
使用 Composer
composer require underpin/berlindb-extension
手动
此插件使用内置的自动加载器,因此只要在扩展之前需要 BerlinDB,它应该能按预期工作。
require_once(__DIR__ . '/underpin-berlin-db/underpin-berlin-db.php');
设置
- 安装 BerlinDB
- 安装 Underpin。请参阅 Underpin 文档
- 创建 BerlinDB 类。
- 根据需要注册新的数据库模型。
示例
如果你目前已经构建了所有 BerlinDB 类,你可以直接引用它们,如下所示。这将创建一个新的数据库模型 example
,可以通过 underpin()->berlin_db()->get('example')
来引用。
underpin()->berlin_db()->add( 'example', [ 'table' => 'Namespace\To\Berlin_DB\Table', 'schema' => 'Namespace\To\Berlin_DB\Schema', 'query' => 'Namespace\To\Berlin_DB\Query', 'name' => 'Human Readable Table Name', 'description' => 'Description of the purpose of this table', 'sanitize_callback' => function( $key, $value ){ // Function to sanitize fields before saving. } ] );
或者,你可以扩展 Database_Model
并直接引用扩展后的类,如下所示
underpin()->berlin_db()->add('database-model-key','Namespace\To\Class');
使用模型
一旦注册,你可以使用各种辅助方法访问模型内部的任何类。
// Run a query using the specified model underpin()->berlin_db()->get('example')->query([/*...*/]); // Get table object underpin()->berlin_db()->get('example')->table(); // Get schema underpin()->berlin_db()->get('example')->schema();
创建、更新和删除
模型包含一些辅助函数,使更新数据更加容易。
// Automatically sanitize, and then create/update a record. // If the provided arguments include an ID, it will update that record. // Otherwise, it will simply create a new record. $id = underpin()->berlin_db()->get('example')->save( [/*...*/] ); // Delete a record $deleted = underpin()->berlin_db()->get('example')->delete( $id );
表设置
// Install all tables underpin()->berlin_db()->install(); // Reset all tables underpin()->berlin_db()->reset(); // Delete all tables underpin()->berlin_db()->uninstall();
元数据表
如果数据库模型也有元数据表,可以指示模型使该表在模型中可访问。为此,你只需在注册模型时使用 Database_Model_With_Meta_Instance
。
// Meta Table underpin()->berlin_db()->add( 'example-meta-table', [ 'table' => 'Namespace\To\Berlin_DB\Table', 'schema' => 'Namespace\To\Berlin_DB\Schema', 'query' => 'Namespace\To\Berlin_DB\Query', 'name' => 'Human Readable Table Name', 'description' => 'Description of the purpose of this table', 'sanitize_callback' => function( $key, $value ){ // Function to sanitize fields before saving. } ] ); // Table underpin()->berlin_db()->add( 'example', [ 'class' => 'Underpin_BerlinDB\Factories\Database_Model_With_Meta_Instance', 'args' => [ 'table' => 'Namespace\To\Berlin_DB\Table', 'schema' => 'Namespace\To\Berlin_DB\Schema', 'query' => 'Namespace\To\Berlin_DB\Query', 'name' => 'Human Readable Table Name', 'description' => 'Description of the purpose of this table', 'sanitize_callback' => function( $key, $value ){ // Function to sanitize fields before saving. }, 'get_meta_table_callback' => function(){ return underpin()->berlin_db()->get('example-meta-table'); } ] ] );
或者,你可以扩展 Database_Model
,使用 With_Meta
特性,然后直接引用扩展后的类,如下所示
underpin()->berlin_db()->add('database-model-key','Namespace\To\Class\Using\With_Meta\Trait');
有了这种设置,你现在可以在 example
模型的上下文中访问一些其他方法。
//add_meta underpin()->berlin_db()->get('example')->add_meta(/*...*/); //update_meta underpin()->berlin_db()->get('example')->update_meta(/*...*/); //delete_meta underpin()->berlin_db()->get('example')->delete_meta(/*...*/); //get_meta underpin()->berlin_db()->get('example')->get_meta(/*...*/);