openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

DBVersion000031.class.php (1591B)


      1 <?php
      2 
      3 namespace cms\update\version;
      4 
      5 use cms\base\Startup;
      6 use database\DbVersion;
      7 use database\Column;
      8 use security\Password;
      9 
     10 /**
     11  * Migrate file values to content.
     12  *
     13  * @author dankert
     14  *
     15  */
     16 class DBVersion000031 extends DbVersion
     17 {
     18     /**
     19      *
     20      */
     21     public function update()
     22     {
     23     	$contentTable = $this->table('content'       );
     24     	$valueTable   = $this->table('value'         );
     25 
     26     	$fileTable = $this->table('file' );
     27     	$fileTable->column('contentid')->add();
     28 
     29 		$db    = $this->getDb();
     30 		$stmt  = $db->sql('SELECT id FROM '.$fileTable->getSqlName() );
     31 
     32 		foreach($stmt->getCol() as $fileid )
     33 		{
     34 			$stmt = $db->sql('SELECT * FROM '.$fileTable->getSqlName().' WHERE id='.$fileid );
     35 			$row = $stmt->getRow();
     36 
     37 			$stmt = $db->sql('SELECT MAX(id) FROM '.$contentTable->getSqlName() );
     38 			$contentid = $stmt->getOne() + 1;
     39 
     40 			$stmt = $db->sql('INSERT INTO '.$contentTable->getSqlName().' (id) VALUES('.$contentid.')') ;
     41 			$stmt->execute();
     42 
     43 			$stmt = $db->sql('SELECT MAX(id) FROM '.$valueTable->getSqlName() );
     44 			$valueid = $stmt->getOne() + 1;
     45 
     46 			$stmt = $db->sql('INSERT INTO '.$valueTable->getSqlName().' (id,contentid,active,publish,file,lastchange_date) VALUES('.$valueid.','.$contentid.',1,1,{file},{time})');
     47 			$stmt->setString( 'file', $row['value'] );
     48 			$stmt->setInt   ( 'time', 0 );
     49 			$stmt->execute();
     50 
     51 			$stmt = $db->sql('UPDATE '.$fileTable->getSqlName().' SET contentid='.$contentid.' WHERE id='.$row['id']);
     52 			$stmt->execute();
     53 		}
     54 
     55 		$fileTable->addConstraint('contentid' , 'content' );
     56 		$fileTable->column('value')->drop();
     57 
     58 	}
     59 }
     60