Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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