This website recently got a major rebuild; if you're missing something, let Lorna know.

Insert Data with Phinx



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. I like Phinx because it avoids the numbered-patches problem by using date stamps as part of its patch naming and it is pretty smart about creating the correct forward and backward changesets from a single change() description.

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. It's actually simple to do so here's the example (so I can find it again!):

$roles_table = $this->table('roles');
$roles_table // id created automatically
    ->addColumn('name', 'string')
    ->create();

$roles_table->insert(["name"], [["admin"]]);
$roles_table->saveData();

The only gotcha here is that when changing a table structure you call save() and when saving data you call saveData(). The seed data is ideal for content, but if you do want to insert data along with a structure change, this is a good way to do it.


In: php
Tags: #database version control #phinx