Elgg  Version master
20171006131622_drop_groups_entity_table.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class DropGroupsEntityTable extends AbstractMigration
6 {
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  $col_names = "'" . implode("', '", $cols) . "'";
19 
20  $groups_query = "SELECT * FROM {$prefix}groups_entity LIMIT 100";
21  while ($rows = $this->fetchAll($groups_query)) {
22  $guids = [];
23  foreach ($rows as $row) {
24  $guids[] = $row['guid'];
25  }
26 
27  $guids = implode(',', $guids);
28 
29  // remove existing metadata... attributes are more important
30  $this->execute("
31  DELETE FROM {$prefix}metadata
32  WHERE entity_guid IN ({$guids}) AND
33  name IN ({$col_names})
34  ");
35 
36  $new_metadata_rows = [];
37 
38  foreach ($rows as $row) {
39  foreach ($cols as $col) {
40  $value = $row[$col];
41  if (is_null($value) || $value === '') {
42  continue;
43  }
44 
45  $new_metadata_rows[] = [
46  'entity_guid' => $row['guid'],
47  'name' => $col,
48  'value' => $value,
49  'value_type' => 'text',
50  'owner_guid' => 0,
51  'access_id' => 2,
52  'time_created' => time(),
53  'enabled' => 'yes',
54  ];
55  }
56  }
57 
58  if (!empty($new_metadata_rows)) {
59  $this->table('metadata')->insert($new_metadata_rows)->saveData();
60  }
61 
62  // remove from groups so it does not get processed again in the next while loop
63  $this->execute("
64  DELETE FROM {$prefix}groups_entity
65  WHERE guid IN ({$guids})
66  ");
67  }
68 
69  // all data migrated, so drop the table
70  $this->table('groups_entity')->drop()->save();
71  }
72 }
$rows
Definition: redis.php:25
up()
Move groups_entity attributes to metadata.
$value
Definition: generic.php:51
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9