lorddashme / wordpress-db-schema-extender
A WordPress Database extender that provides a nice structure of table schema and data seeds.
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
A WordPress Database extender that provides a nice structure of table schema and data seeds.
Requirement(s)
- PHP version from 5.6.* up to latest.
Install
- It is advice to install the package via Composer. Use the command below to install the package:
composer require lorddashme/wordpress-db-schema-extender
Usage
-
You can start using the package without any configuration needed.
-
Below are the available functions:
- Below are the sample implementation:
<?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(); } );
- You can also use the SchemaExtender class like static-like class. See the "use" namespace path or imported class used.
<?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);
Seed Table
- The SchemaExtender class "tableSeed" function is not only for closure type in the second argument. Also you can use array type in the second argument.
<?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);
Drop Table
- The SchemaExtender class also provide a "dropTable" or "dropTables" function to accomodate the drop table action.
<?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']);
License
- This package is open-sourced software licensed under the MIT license.