Completed
Push — master ( fab0a4...8e249d )
by Jeroen
41:43 queued 16:43
created

20171006131622_drop_groups_entity_table.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
5 View Code Duplication
class DropGroupsEntityTable extends AbstractMigration
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    /**
8
     * Move groups_entity attributes to metadata
9
     */
10
    public function up() {
11
12
		if (!$this->hasTable('groups_entity') || !$this->hasTable('metadata')) {
13
			return;
14
		}
15
16
		$prefix = $this->getAdapter()->getOption('table_prefix');
17
		$cols = ['name', 'description'];
18
		
19
		$groups_query = "SELECT * FROM {$prefix}groups_entity LIMIT 25";
20
		while ($rows = $this->fetchAll($groups_query)) {
21
			foreach ($rows as $row) {
22
				foreach ($cols as $col) {
23
					
24
					// remove existing metadata... attributes are more important
25
					$this->execute("
26
						DELETE FROM {$prefix}metadata
27
						WHERE entity_guid = {$row['guid']} AND
28
						name = '{$col}'
29
					");
30
					
31
					$this->insert('metadata', [
32
						'entity_guid' => $row['guid'],
33
						'name' => $col,
34
						'value' => $row[$col],
35
						'value_type' => 'text',
36
						'owner_guid' => 0,
37
						'access_id' => 2,
38
						'time_created' => time(),
39
						'enabled' => 'yes',
40
					]);
41
				}
42
				
43
				// remove from groups so it does not get processed again in the next while loop
44
				$this->execute("
45
					DELETE FROM {$prefix}groups_entity
46
					WHERE guid = {$row['guid']}
47
				");
48
			}
49
		}
50
		
51
		// all data migrated, so drop the table
52
		$this->dropTable('groups_entity');
53
    }
54
}
55