Archived
1
0
Fork 0
This repository has been archived on 2026-04-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
httpcb/app/migrations/20180814172405_user_split_name.php

45 lines
1.3 KiB
PHP

<?php
use Phinx\Migration\AbstractMigration;
class UserSplitName extends AbstractMigration
{
public function up()
{
$this->getAdapter()->beginTransaction();
// Rename "name" to "firstname" and add "lastname".
$this->table('user')
->renameColumn('name', 'firstname')
->addColumn('lastname','string', [
'length' => 128,
'after' => 'firstname',
'null' => true
])
->save();
// Update row data, moving everything after first space to from lastname.
foreach($this->fetchAll("SELECT `id`,`firstname` FROM `user`") as $row) {
$builder = $this->getQueryBuilder()->update('user')
->where(['id' => $row['id']]);
$firstname = $row['firstname'];
// If we find a space.
$pos = strpos($firstname, ' ');
if ($pos !== false) {
// Set everything after the first space to lastname.
$builder->set('lastname', substr($firstname, $pos+1));
// Remove everything after first space from firstname.
$firstname = substr($firstname, 0, $pos);
}
$builder->set('firstname', $firstname);
$builder->execute();
}
$this->getAdapter()->commitTransaction();
}
}