2015-11-12 52 views
0

在app \ tmp目錄\緩存\模型有一個名爲是如何從CakePHP模型緩存恢復MySQL數據庫?</p> <pre><code>myapp_cake_model_default_mydb_users </code></pre> <p>內容是:

1446583948 
a:14:{s:2:"id";a:6:{s:4:"type";s:7:"integer";s:4:"null";b:0;s:7:"default";N;s:6:"length";i:11;s:8:"unsigned";b:0;s:3:"key";s:7:"primary";}s:8:"username";a:7:{s:4:"type";s:6:"string";s:4:"null";b:0;s:7:"default";N;s:6:"length";i:255;s:3:"key";s:5:"index";s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:8:"password";a:6:{s:4:"type";s:6:"string";s:4:"null";b:1;s:7:"default";N;s:6:"length";i:255;s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:17:"num_free_listings";a:5:{s:4:"type";s:7:"integer";s:4:"null";b:1;s:7:"default";s:1:"0";s:6:"length";i:2;s:8:"unsigned";b:0;}s:3:"pin";a:6:{s:4:"type";s:6:"string";s:4:"null";b:1;s:7:"default";N;s:6:"length";i:255;s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:7:"is_ldap";a:4:{s:4:"type";s:7:"boolean";s:4:"null";b:0;s:7:"default";N;s:6:"length";i:1;}s:13:"ldap_username";a:6:{s:4:"type";s:6:"string";s:4:"null";b:1;s:7:"default";N;s:6:"length";i:255;s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:8:"fullname";a:6:{s:4:"type";s:6:"string";s:4:"null";b:0;s:7:"default";N;s:6:"length";i:255;s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:8:"group_id";a:5:{s:4:"type";s:7:"integer";s:4:"null";b:0;s:7:"default";s:1:"2";s:6:"length";i:11;s:8:"unsigned";b:0;}s:16:"password_changed";a:4:{s:4:"type";s:4:"date";s:4:"null";b:1;s:7:"default";N;s:6:"length";N;}s:10:"last_login";a:4:{s:4:"type";s:8:"datetime";s:4:"null";b:1;s:7:"default";N;s:6:"length";N;}s:6:"status";a:6:{s:4:"type";s:26:"enum('active','suspended')";s:4:"null";b:0;s:7:"default";s:6:"active";s:6:"length";i:9;s:7:"collate";s:15:"utf8_general_ci";s:7:"charset";s:4:"utf8";}s:7:"created";a:4:{s:4:"type";s:8:"datetime";s:4:"null";b:0;s:7:"default";N;s:6:"length";N;}s:8:"modified";a:4:{s:4:"type";s:8:"datetime";s:4:"null";b:0;s:7:"default";N;s:6:"length";N;}} 

我可以轉換成這個(?系列化PHP)到SQL並重新創建我的表,如果是的話如何?提前謝謝了。

回答

0

在PHP中創建一個文件:

<?php 
class AppSchema extends CakeSchema { 

public function before($event = array()) { 
    return true; 
} 

public function after($event = array()) { 
}  

$dir = 'files/'; 
$files = [ 
    'table_names' => 'myapp_cake_model_default_mydb_tablenames', 

]; 
foreach ($files as $table => $file){ 
$content = file(realpath($dir.$file)); 
$content = unserialize($content[1]); 
$info = null; 
foreach ($content as $field => $prop){ 
    $info .= "'$field' => array("; 
     foreach ($prop as $key => $value){ 
      $info .= "'$key' => "; 
      if($key == 'null'){ 
       $info .= empty($value)?'false':'true'; 
      }elseif($key == 'default'){ 
       $info .= empty($value)?'null':"'$value'"; 
      }elseif($key == 'unsigned'){ 
       $info .= empty($value)?'false':'true'; 
      }else{ 
       $info .= empty($value)?"''":"'$value'"; 
      } 
      $info .= ", "; 
     } 
    $info .= "),\n"; 
} 
echo "public $".$table." = array(
    ".$info."'indexes' => array(
     'PRIMARY' => array('column' => 'id', 'unique' => 1), 
    ), 
    'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB') 
);\n\n"; 
} 
?> 
} 

然後在瀏覽器中運行它,你應該得到一些代碼粘貼在schema.php文件。然後使用Cake控制檯在MySQL數據庫中創建表。