public function writeSchema($options)
{
//App::uses('CakeSchema', 'Model');
extract($options);
if (!isset($model)) {
return false;
}
// 登録済のクラスをクリアする
// 何故かプラグインのモデルがコアのDB設定で登録されてしまっているため
// コアとプラグインを連続して書き出すとプラグインのテーブルが見つからない
ClassRegistry::flush();
if (!isset($file)) {
if (is_array($model)) {
$basename = $this->configKeyName;
} else {
$basename = Inflector::tableize($model);
$model = array($model);
}
$file = $basename . '.php';
} else {
$basename = basename($file, '.php');
}
if (!isset($name)) {
$name = Inflector::camelize($basename);
}
$Schema = ClassRegistry::init('CakeSchema');
if (!empty($plugin)) {
$Schema->plugin = $plugin;
}
if (isset($connection)) {
$Schema->connection = $connection;
} else {
$Schema->connection = $this->configKeyName;
}
if (!isset($path)) {
$path = $Schema->path;
}
// CakeSchema では、hasAndBelongsToMany に設定されているモデルも同時に書き出す仕様となっている為、
// 書き出さないように変更した。
// バックアップファイルの生成で問題が発生した為
foreach ($model as $value) {
$Object = ClassRegistry::init(array('class' => $value, 'ds' => $Schema->connection));
$Object->hasAndBelongsToMany = array();
}
$this->cacheSources = false;
$options = $Schema->read(array('models' => false));
if (!empty($options['tables'][$basename])) {
$options = array('tables' => array($basename => $options['tables'][$basename]));
}
$options = array_merge($options, array('name' => $name, 'file' => $file, 'path' => $path));
$result = $Schema->write($options);
// 不要コード削除、改行コードをLFに変更
$File = new File($path . DS . $file);
$data = $File->read();
$data = str_replace(array("\r\n", "\r"), "\n", $data);
$data = preg_replace('/\\t(var|public)\\s\\$path.+;\\n\\n/', '', $data);
$File->write($data);
return $result;
}