Skip to content

Instantly share code, notes, and snippets.

@webholics
Created August 9, 2013 07:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webholics/6191779 to your computer and use it in GitHub Desktop.
Save webholics/6191779 to your computer and use it in GitHub Desktop.
Processwire + phpmig - This gist shows how to use the concept of migrations with the phpmig library in Processwire to enable versioning of template and field structure and to enable collaboration in Processwire projects.
<?php
use Phpmig\Migration\Migration;
class ContactTemplate extends Migration
{
/**
* Do the migration
*/
public function up()
{
$c = $this->getContainer();
$field = new Field();
$field->name = 'contact_email';
$field->label = 'Recipient email adress';
$field->description = 'The email adress which receives contact form submissions.';
$field->type = 'email';
$field->required = 1;
$field->save();
$field = new Field();
$field->name = 'contact_lat';
$field->label = 'Coordinate Latitude';
$field->description = 'Used for Google Maps.';
$field->type = 'text';
$field->required = 1;
$field->columnWidth = 50;
$field->save();
$field = new Field();
$field->name = 'contact_lng';
$field->label = 'Coodinate Longitude';
$field->description = 'Used for Google Maps';
$field->type = 'text';
$field->required = 1;
$field->columnWidth = 50;
$field->save();
$field = new Field();
$field->name = 'contact_form_text';
$field->label = 'Contact Form Text';
$field->type = 'textarea';
$field->textformatters = array(
'TextformatterEntities',
'TextformatterNewlineBR'
);
$field->save();
$fieldgroup = new Fieldgroup();
$fieldgroup->name = 'contact';
$fieldgroup->add('title');
$fieldgroup->add('headline');
$fieldgroup->add('contact_lat');
$fieldgroup->add('contact_lng');
$fieldgroup->add('contact_email');
$fieldgroup->add('contact_form_text');
$fieldgroup->save();
$template = new Template();
$template->name = 'contact';
$template->fieldgroup = $fieldgroup;
$template->save();
}
/**
* Undo the migration
*/
public function down()
{
$c = $this->getContainer();
$template = $c['wire']->templates->get('contact');
$c['wire']->templates->delete($template);
$fieldgroup = $c['wire']->fieldgroups->get('contact');
$c['wire']->fieldgroups->delete($fieldgroup);
$field = $c['wire']->fields->get('contact_email');
$c['wire']->fields->delete($field);
$field = $c['wire']->fields->get('contact_lat');
$c['wire']->fields->delete($field);
$field = $c['wire']->fields->get('contact_lng');
$c['wire']->fields->delete($field);
$field = $c['wire']->fields->get('contact_form_text');
$c['wire']->fields->delete($field);
}
}
<?php
use \Phpmig\Adapter,
\Phpmig\Pimple\Pimple;
// include Processwire to get DB config
include __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
$container = new Pimple();
$container['wire'] = $wire;
$container['db'] = $container->share(function() use ($container) {
return new PDO(
'mysql:' .
'dbname=' . $container['wire']->config->dbName . ';' .
'host=' . $container['wire']->config->dbHost . ';' .
'port=' . $container['wire']->config->dbPort,
$container['wire']->config->dbUser,
$container['wire']->config->dbPass
);
});
$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\PDO\Sql($container['db'], 'migrations');
});
$container['phpmig.migrations'] = function() {
return glob(__DIR__ . DIRECTORY_SEPARATOR . 'migrations/*.php');
};
return $container;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment