Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Helge Sverre:
Database migrations in PHP with Phinx
Jul 04, 2016 @ 11:14:55

Helge Sverre has put together an introduction of a tool perfect for anyone that's been looking for a framework-agnostic way to handle database migrations: Phinx.

Phinx is a database migration tool written by Rob Morgan in PHP, what that means is that you can tell Phinx that you want to create a new database table, add a column or edit the properties of a column by writing “migrations”.

You can then run this migration using the Phinx tool and it will connect to your database with the configuration that you specified and perform the database updates for you automatically.

He then walks you through a full introduction to the tool, breaking it down into sections:

  • Getting Phinx installed (via Composer)
  • Configuring it via a YAML file
  • Writing your first simple migrations
  • Creating database seeders (including the use of Faker data)
  • Rolling back your migrations

All of these topics come with plenty of code, configuration and output examples, helping you ensure you're on the right track.

tagged: database migration phinx library introduction faker tutorial

Link: https://helgesverre.com/blog/database-migrations-in-php-with-phinx/

SitePoint PHP Blog:
Phinx – the Migration Library You Never Knew You Needed
Jun 02, 2016 @ 13:15:36

The SitePoint PHP blog has posted a tutorial about the migration library you never knew you needed for your PHP applications - Phinx (for database migrations).

You’re building an application, and you need to share the database’s structure with your team. After all, you want everyone to be up and running as soon as possible. What do you do? SQL dumps of table structures? You could… but that’s so very primitive – and can be time consuming to import, too! More often than not, database migrations are the answer.

In this tutorial, we’ll go through a framework-agnostic package for building and executing database migrations called Phinx.

He starts by helping you get the tool installed via Composer and initializing your environment for migrations and seeds. He covers the configuration of the tool via a simple YAML file and the creation of first migration files based on an existing table structure. He walks through the code involved to create the tables, add indexes and foreign keys. He shows how to run the migrations, gives an example of the error output and modifying the current database structure.

tagged: migration phinx library tutorial agnostic introduction

Link: https://www.sitepoint.com/phinx-the-migration-library-you-never-knew-you-needed/

Johannes Siipola:
How to use Eloquent ORM migrations outside Laravel
Jan 20, 2016 @ 11:55:10

On his site Johannes Siipola has posted a tutorial showing how to use the Eloquent ORM outside of Laravel, more specifically the migration functionality to manage database table creation/updates/deletions.

Laravel's Eloquent is one of the most fluent and expressive ORM's available for PHP. However, unlike some of its competitors like Doctrine or Propel, it's highly tight to the Laravel ecosystem in some ways. It's still possible to use Eloquent outside Laravel with a bit of work.

[...] If you use Eloquent outside Laravel, you will be missing one of it's best features: database migrations. [...] There has been some effort in order to use Laravel migrations outside the framework, but it hasn't been possible because the migrations require a complete instance of Laravel to run. In this tutorial, we are using framework agnostic Phinx migration library instead.

He then shows how to install the packages needed for the example: Slim framework, the Eloquent ORM and Phinx. He then shows how to set up the database and Phinx configurations and create your first Phinx migration. Inside this migration he makes use of the Eloquent schema builder functionality to create a simple "widgets" table. He ends the post showing you how to create the simple Slim application and work with the model they've created to both save and read data easily.

tagged: eloquent laravel outside orm migration phinx slimframework tutorial

Link: https://siipo.la/blog/how-to-use-eloquent-orm-migrations-outside-laravel

Lorna Mitchell:
Insert Data with Phinx
Dec 18, 2015 @ 10:49:07

Lorna Mitchell has a new post to her site with a quick tip for any of the Phinx users out there around a new feature they've added: database seeding.

Database patching is a wicked hard problem, one that's got a bit easier in my world lately as I've been using Phinx on a few projects. [...] One thing I didn't immediately find was how to insert data. Phinx has seed functionality but in this case I needed to put in a lookup table to go along with a data structure change.

She includes a code examples of this new feature, showing how to create a roles table and seed it with a new record with a "name" value of "admin". She also mentions one "gotcha" in the name of the function used to save the data to the new table (saveData versus just save).

tagged: phinx insert data database migration example tutorial

Link: http://www.lornajane.net/posts/2015/insert-data-with-phinx

Rob Morgan:
Getting Started With Phinx
Oct 18, 2012 @ 12:30:49

Rob Morgan has a recent post showing you how to use Phinx, a PHP-based database migration tool, to handle your database changes (both "up" and "down").

Earlier this year I decided to open-source one of my personal software projects - Phinx. Phinx is a database migration tool (think Ruby on Rails ActiveRecord migrations) where can you describe all of your database transformations in pure PHP. I have used this tool for many of my consulting projects, however it still took a lot of effort to turn it into a re-usable product. In this tutorial I'm going to explain how to install Phinx and use it with a simple guestbook application.

He uses a sample guestbook project for his example, showing how to get it all installed and how to create a migration that adds a "posts" table.

tagged: phinx database migration tutorial introduction guestbook