alexstandiford/underpin-berlindb

此包已被废弃,不再维护。作者建议使用 underpin/berlindb-extension 包。

Underpin 的 BerlinDB 实现

1.3.0 2021-11-23 14:21 UTC

This package is auto-updated.

Last update: 2021-11-23 16:18:24 UTC


README

BerlinDB 集成到 Underpin WordPress 框架。

一些关键优势

  1. 通过 Database_Model 类注册表。此类提供了有用的上下文,使使用 BerlinDB 变得更容易,同时帮助保持您的代码 DRY。
  2. 所有表都存储在注册表中。这允许您一次与所有表交互。需要卸载所有内容?您可以使用一个方法来完成,而不是手动遍历表。
  3. 所有 CRUD 操作都通过数据库模型发生,而不是在所有地方创建 Query 实例。

安装

使用 Composer

composer require underpin/berlindb-extension

手动

此插件使用内置的自动加载器,因此只要在扩展之前需要 BerlinDB,它应该会按预期工作。

require_once(__DIR__ . '/underpin-berlin-db/underpin-berlin-db.php');

设置

  1. 安装 BerlinDB
  2. 安装 Underpin。请参阅 Underpin 文档
  3. 创建 BerlinDB 类。
  4. 根据需要注册新的数据库模型。

示例

如果您目前已经构建了所有 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(/*...*/);