lorddashme / wordpress-db-schema-extender
WordPress 数据库扩展器,提供良好的表结构和数据种子结构。
1.1.0
2018-09-28 10:05 UTC
Requires
- php: >=5.6 || >=7.0 || >=7.1
- lorddashme/php-static-class-interface: 1.*
Requires (Dev)
- mockery/mockery: 1.0.*
- phpunit/phpunit: 5.* || 6.* || 7.*
README
WordPress 数据库扩展器,提供良好的表结构和数据种子结构。
要求
- PHP版本从5.6.*到最新版。
安装
- 建议通过Composer安装此包。使用以下命令安装包
composer require lorddashme/wordpress-db-schema-extender
使用方法
-
您可以在无需任何配置的情况下开始使用此包。
-
以下为可用的函数
- 以下为示例实现
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\Wordpress\DB\SchemaExtender; $schemaExtender = new SchemaExtender(); $schemaExtender->init(function($context) { $context->table('users', function($table) { $table->column('id', 'INT(11) NOT NULL AUTO_INCREMENT'); $table->column('name', 'TEXT NULL'); $table->primaryKey('id'); }); $context->table('user_options', function($table) { $table->column('id', 'INT(11) NOT NULL AUTO_INCREMENT'); $table->column('user_id', 'INT(11) NOT NULL'); $table->column('nick_name', 'INT(11) NOT NULL'); $table->primaryKey('id'); }); $context->rawQuery(' ALTER TABLE ' . $context->tableName('users_options') . ' ADD KEY `user_id` (`user_id`); ALTER TABLE ' . $context->tableName('users_options') . ' ADD CONSTRAINT `foreign_constraint_users_option_users` FOREIGN KEY (`user_id`) REFERENCES ' . $context->tableName('users') . ' (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;' ); $context->tableSeed('users', function($data) { $data->name = 'John Doe'; return $data; }); }); $schemaExtender->tableSeed('user_options', function($data) { $data->user_id = 1; $data->nick_name = 'Nick Name' . rand(); return $data; })->iterate(2); // You can attach the "migrate" function to "register_activation_hook" of wordpress. // When the wordpress plugin set to active you can add the extender "migrate" function // to execute all the query stored before the activation begin. register_activation_hook( '<wordpress>/wp-content/plugins/<your-plugin-name>/<your-plugin-name>.php', function () use ($schemaExtender) { $schemaExtender->migrate(); } );
- 您也可以像静态类一样使用SchemaExtender类。请参阅“use”命名空间路径或导入的类。
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\Wordpress\DB\Facade\SchemaExtender; SchemaExtender::init(function($context) { $context->table('users', function($table) { $table->column('id', 'INT(11) NOT NULL AUTO_INCREMENT'); $table->column('name', 'TEXT NULL'); $table->primaryKey('id'); }); $context->table('user_options', function($table) { $table->column('id', 'INT(11) NOT NULL AUTO_INCREMENT'); $table->column('user_id', 'INT(11) NOT NULL'); $table->column('nick_name', 'INT(11) NOT NULL'); $table->primaryKey('id'); }); $context->rawQuery(' ALTER TABLE ' . $context->tableName('users_options') . ' ADD KEY `user_id` (`user_id`); ALTER TABLE ' . $context->tableName('users_options') . ' ADD CONSTRAINT `foreign_constraint_users_option_users` FOREIGN KEY (`user_id`) REFERENCES ' . $context->tableName('users') . ' (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;' ); }); SchemaExtender::tableSeed('users', function($data) { $data->name = 'John Doe'; return $data; }); SchemaExtender::tableSeed('user_options', function($data) { $data->user_id = 1; $data->nick_name = 'Nick Name' . rand(); return $data; })->iterate(2);
种子表
- SchemaExtender类的"tableSeed"函数不仅适用于第二个参数中的闭包类型,也可以在第二个参数中使用数组类型。
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\Wordpress\DB\SchemaExtender; $schemaExtender = new SchemaExtender(); $schemaExtender->init(); $schemaExtender->tableSeed('users', [ 'name' => 'John Doe', ]); $schemaExtender->tableSeed('user_options', [ 'user_id' => 1, 'nick_name' => 'Nick Name' . rand() ])->iterate(2);
删除表
- SchemaExtender类还提供"dropTable"或"dropTables"函数,以适应删除表的操作。
<?php include __DIR__ . '/vendor/autoload.php'; use LordDashMe\Wordpress\DB\SchemaExtender; $schemaExtender = new SchemaExtender(); $schemaExtender->init(); $schemaExtender->dropTable('users'); $schemaExtender->dropTable('user_options'); // Or you can also use the alias function that support multiple table names in a single argument. $schemaExtender->dropTables(['users', 'user_options']);
许可证
- 此包是开源软件,许可协议为MIT许可证。