12.14 php
AD
Note
Inherited a db which was supposed to be utf8 but full of latin1...
define the following db connections:

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'me',
'password' => 'password',
'database' => 'same',
'prefix' => '',
'encoding' => 'UTF8'
);

var $wrong = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'me',
'password' => 'password',
'database' => 'same',
'prefix' => '',
'encoding' => 'latin1'
);

type "cake fix_charset".

There are other ways if the speed of the script is a problem: http://www.mysqlperformanceblog.com/2007/12/18/fixing-column-encoding-mess-in-mysql/ but
  1. <?php
  2. App::import('Model', array('ConnectionManager', 'Model'), false);
  3. class FixCharsetShell extends Shell  {
  4.     function main() {
  5.  
  6.         $db =& ConnectionManager::getDataSource('default');
  7.         $tables = $db->listSources();
  8.         $Right = new Model(null, $tables[0], 'default');
  9.         $Wrong = new Model(null, $tables[0] , 'wrong');
  10.  
  11.         foreach($tables as $table) {
  12.             $this->hr();
  13.             $this->out("Processing $table");
  14.             $Wrong->setSource($table);
  15.             $Right->setSource($table);
  16.             $page = 1;
  17.             $limit = 100;
  18.             while($data = $Wrong->find('all', compact('page', 'limit'))) {
  19.                 $this->out(sprintf("\t... rows %s to %s",
  20.                     ((($page - 1) * $limit) + 1),
  21.                     min(($page * $limit), (($page -1) * $limit + count($data)))
  22.                 ));
  23.                 foreach($data as $row) {
  24.                     $Right->create();
  25.                     $Right->save($row['Model']);
  26.                 }   
  27.                 $page++;
  28.             }   
  29.         }   
  30.     }   
  31. }
  32.  
Parsed in 0.044 seconds, using GeSHi 1.0.7.14

Modify this Paste