DBVersion000009.class.php (1727B)
1 <?php 2 3 4 namespace cms\update\version; 5 6 use cms\model\BaseObject; 7 use database\DbVersion; 8 use database\Column; 9 use security\Password; 10 11 /** 12 * New Object type 'url'. 13 * 14 * In this Version 9 we are creating a table 'url' and are copying 15 * the selected entries from table 'link' to 'url'. 16 * 17 * @author dankert 18 * 19 */ 20 class DBVersion000009 extends DbVersion 21 { 22 /** 23 * 24 */ 25 public function update() 26 { 27 // Creating new table 'url' 28 $table = $this->table('url')->add(); 29 $table->column('objectid')->type(Column::TYPE_INT)->size(0)->add(); 30 $table->column('url')->type(Column::TYPE_VARCHAR)->size(255)->add(); 31 32 $table->addPrimaryKey(); 33 $table->addConstraint('objectid', 'object'); 34 35 $table->addUniqueIndex('objectid'); 36 37 // Copying values from table 'link' to new table 'url' 38 $db = $this->getDb(); 39 40 $insertStmt = $db->sql('INSERT INTO '.$table->getSqlName(). 41 ' (id,objectid,url) SELECT id,objectid,url FROM '.$this->table('link')->getSqlName().' WHERE url is not null' 42 ); 43 $insertStmt->execute(); 44 45 // Updating the typeid for URL entrys in table 'object' 46 $updateStmt = $db->sql('UPDATE '.$this->table('object')->getSqlName(). 47 ' SET typeid='.BaseObject::TYPEID_URL.' WHERE id IN (SELECT objectid FROM '.$this->table('url')->getSqlName().')' 48 ); 49 $updateStmt->execute(); 50 51 $tableLink = $this->table('link'); 52 // Remove old entrys in table 'link' 53 $updateStmt = $db->sql('DELETE FROM '.$tableLink->getSqlName().' WHERE url is not null' 54 ); 55 $updateStmt->execute(); 56 57 // Cleanup: Drop unused column. 58 $tableLink->column('url')->drop(); 59 } 60 }