Elgg  Version 3.0
20171009115032_drop_objects_entity_table.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class DropObjectsEntityTable extends AbstractMigration {
9  public function up() {
10 
11  if (!$this->hasTable('objects_entity') || !$this->hasTable('metadata')) {
12  return;
13  }
14 
15  $prefix = $this->getAdapter()->getOption('table_prefix');
16  $cols = ['title', 'description'];
17  $col_names = "'" . implode("', '", $cols) . "'";
18 
19  $objects_query = "SELECT * FROM {$prefix}objects_entity LIMIT 100";
20  while ($rows = $this->fetchAll($objects_query)) {
21  $guids = [];
22  foreach ($rows as $row) {
23  $guids[] = $row['guid'];
24  }
25 
26  $guids = implode(',', $guids);
27 
28  // remove existing metadata... attributes are more important
29  $this->execute("
30  DELETE FROM {$prefix}metadata
31  WHERE entity_guid IN ({$guids}) AND
32  name IN ({$col_names})
33  ");
34 
35  $new_metadata_rows = [];
36  foreach ($rows as $row) {
37  foreach ($cols as $col) {
38  $value = $row[$col];
39  if (is_null($value) || $value === '') {
40  continue;
41  }
42 
43  $new_metadata_rows[] = [
44  'entity_guid' => $row['guid'],
45  'name' => $col,
46  'value' => $value,
47  'value_type' => 'text',
48  'owner_guid' => 0,
49  'access_id' => 2,
50  'time_created' => time(),
51  'enabled' => 'yes',
52  ];
53  }
54  }
55 
56  if (!empty($new_metadata_rows)) {
57  $this->insert('metadata', $new_metadata_rows);
58  }
59 
60  // remove from objects so it does not get processed again in the next while loop
61  $this->execute("
62  DELETE FROM {$prefix}objects_entity
63  WHERE guid IN ({$guids})
64  ");
65  }
66 
67  // all data migrated, so drop the table
68  $this->dropTable('objects_entity');
69  }
70 }
$rows
Definition: redis.php:20
up()
Move objects_entity attributes to metadata.
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
$value
Definition: debugging.php:7
elgg ElggPriorityList prototype insert
Inserts an element into the priority list at the priority specified.