GithubHelp home page GithubHelp logo

sitedata / wp-table-migrations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cuisine-wp/wp-table-migrations

0.0 1.0 0.0 33 KB

Create laravel-style database migrations in WordPress

License: Other

PHP 100.00%

wp-table-migrations's Introduction

WP Table Migrations

Gives you the possibility to create Laravel-like database migrations in WordPress.


Requirements

Prerequisite How to check How to install
PHP >= 5.4.x php -v php.net

Installing

Clone the git repo - git clone https://github.com/cuisine-wp/wp-table-migrations.git or install with composer:

composer require chefduweb/wp-table-migrations

After you have all the files you need to install wp-table-migrations like a regular WordPress plugin:

  1. move the files to wp-content/plugins
  2. get into the WordPress admin and go to plugins
  3. activate WP Table Migrations.

Getting Started

Creating database migrations with the power of WP Table Migrations is quite easy; you just create a class that extends

\TableMigrations\Migrations\Migration;

And use WP Cli to run the command:

wp tables migrate

That's it! See our examples for more:


Quick examples

Here are a few quick examples on how WP Table Migrations will be making your life easier:


Creating and dropping custom tables

This actually looks exactly the same as Laravel's database migrations, with the exception of having to extend a different class and create an instance of that class at the bottom.

namespace MyPlugin\Database;

use TableMigrations\Wrappers\Schema;
use TableMigrations\Database\Blueprint;
use TableMigrations\Migrations\Migration;

class ProductMetaMigration extends Migration{


    /**
     * Put the table up
     *
     * @return void
     */
    public function up()
    {
        Schema::create( 'product_meta', function( Blueprint $table ){
            $table->increments( 'id' )->unique();
            $table->integer( 'product_id' )->unsigned();
            $table->string( 'image_url' )->nullable();
            $table->integer( 'position' );
            $table->float( 'price' );
            $table->integer( 'stock' );
        });         
    }

    /**
     * Default down function
     *
     * @return void
     */
    public function down()
    {
        Schema::drop( 'product_meta' );
    }

}


\MyPlugin\Database\ProductMetaMigration::getInstance();

In this example a table named product_meta is created or destroyed, depending on which direction the migration is running.

Creating columns

You can create a lot of different column types with Table Migrations. Here's a list:

Command Description
$table->bigInteger('votes'); BIGINT equivalent for the database.
$table->binary('data'); BLOB equivalent for the database.
$table->boolean('confirmed'); BOOLEAN equivalent for the database.
$table->char('name', 4); CHAR equivalent with a length.
$table->date('created_at'); DATE equivalent for the database.
$table->dateTime('created_at'); DATETIME equivalent for the database.
$table->decimal('amount', 5, 2); DECIMAL equivalent with a precision and scale.
$table->double('column', 15, 8); DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.
$table->float('amount', 8, 2); FLOAT equivalent for the database, 8 digits in total and 2 after the decimal point.
$table->increments('id'); } Incrementing ID (primary key) using a "UNSIGNED INTEGER" equivalent.
$table->integer('votes'); INTEGER equivalent for the database.
$table->longText('description'); LONGTEXT equivalent for the database.
$table->mediumInteger('numbers'); MEDIUMINT equivalent for the database.
$table->mediumText('description'); MEDIUMTEXT equivalent for the database.
$table->smallInteger('votes'); SMALLINT equivalent for the database.
$table->string('email'); VARCHAR equivalent column.
$table->string('name', 100); VARCHAR equivalent with a length.
$table->text('description'); TEXT equivalent for the database.
$table->time('sunrise'); TIME equivalent for the database.
$table->tinyInteger('numbers'); TINYINT equivalent for the database.
$table->timestamp('added_on'); TIMESTAMP equivalent for the database.

Saving & Fetching data

WordPress has all sorts of functions for saving and retrieving your data from the 12 database tables it already knows. We wouldn't want to leave you hanging, so we created a simple wrapper for your custom tables as well.

use TableMigrations\Wrappers\Record;

//inserting:
Record::insert( 'product_meta', $data );

//updating:
Record::update( 'product_meta', $id, $data );

//removing:
Record::delete( 'product_meta', $id );

The record class can handle table inserts, updates and upserts, but also fetches:

//find all product meta associated with this product:
Record::find( 'product_meta' )
        ->where(['product_id' => $product_id ])
        ->results();

//find the first product meta where the price is 0
Record::find( 'product_meta' )
        ->where([

            'product_id' => $product_id,
            'price' => 0

        ])->first();

Adding WHERE clauses results in "AND" queries by default. Currently we're still working on "OR" support. Record::find() queries will return either an Array or null if the query returned no table rows.


Documentation

Documentation is still being worked on at the moment.


Contributing

Everyone is welcome to help contribute and improve this project. There are several ways you can contribute:

  • Reporting issues
  • Suggesting new features
  • Writing or refactoring code
  • Fixing issues

wp-table-migrations's People

Contributors

dannyvankooten avatar lucprincen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.