mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
updating schema reading
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5579 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
2e60a4d2f1
commit
0533bbd92c
3 changed files with 148 additions and 56 deletions
|
@ -159,19 +159,11 @@ class CakeSchema extends Object {
|
||||||
$options
|
$options
|
||||||
));
|
));
|
||||||
$db =& ConnectionManager::getDataSource($connection);
|
$db =& ConnectionManager::getDataSource($connection);
|
||||||
$tables = $db->sources();
|
$currentTables = array_flip($db->sources());
|
||||||
|
|
||||||
|
|
||||||
if (empty($models)) {
|
if (empty($models)) {
|
||||||
$models = Configure::listObjects('model');
|
$models = Configure::listObjects('model');
|
||||||
}
|
}
|
||||||
|
loadModel(null);
|
||||||
$Inflector = Inflector::getInstance();
|
|
||||||
$tablesWithoutModels = array_diff(array_map(array($Inflector, 'classify'), $tables), $models);
|
|
||||||
$modelsWithoutTables = array_diff($models, array_map(array($Inflector, 'classify'), $tables));
|
|
||||||
|
|
||||||
$models = am($tablesWithoutModels, array_diff($models, $modelsWithoutTables));
|
|
||||||
|
|
||||||
$tables = array();
|
$tables = array();
|
||||||
foreach ($models as $model) {
|
foreach ($models as $model) {
|
||||||
if($model == 'ArosAco') {
|
if($model == 'ArosAco') {
|
||||||
|
@ -187,23 +179,38 @@ class CakeSchema extends Object {
|
||||||
if(class_exists(low($model))) {
|
if(class_exists(low($model))) {
|
||||||
$Object =& new $model();
|
$Object =& new $model();
|
||||||
$Object->setDataSource($connection);
|
$Object->setDataSource($connection);
|
||||||
if (is_object($Object)) {
|
if (is_object($Object) && isset($currentTables[$Object->table])) {
|
||||||
if(empty($tables[$Object->table])) {
|
if(empty($tables[$Object->table])) {
|
||||||
$tables[$Object->table] = $this->__columns($Object);
|
$tables[$Object->table] = $this->__columns($Object);
|
||||||
$tables[$Object->table]['indexes'] = $db->index($Object);
|
$tables[$Object->table]['indexes'] = $db->index($Object);
|
||||||
|
unset($currentTables[$Object->table]);
|
||||||
}
|
}
|
||||||
if(!empty($Object->hasAndBelongsToMany)) {
|
if(!empty($Object->hasAndBelongsToMany)) {
|
||||||
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
|
foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
|
||||||
$class = $assocData['className'];
|
if (isset($assocData['with'])) {
|
||||||
$tables[$Object->$Assoc->table] = $this->__columns($Object->$class);
|
$class = $assocData['with'];
|
||||||
$tables[$Object->$Assoc->table]['indexes'] = $db->index($Object->$class);
|
} elseif ($assocData['_with']) {
|
||||||
|
$class = $assocData['_with'];
|
||||||
}
|
}
|
||||||
}
|
if (is_object($Object->$class) && isset($currentTables[$Object->$class->table])) {
|
||||||
} else {
|
$tables[$Object->$class->table] = $this->__columns($Object->$class);
|
||||||
trigger_error('Schema generation error: model class ' . $class . ' not found', E_USER_WARNING);
|
$tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
|
||||||
|
unset($currentTables[$Object->$class->table]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($currentTables)) {
|
||||||
|
foreach(array_flip($currentTables) as $table) {
|
||||||
|
$Object = new AppModel(array('name'=> Inflector::classify($table), 'table'=> $table, 'ds'=> $connection));
|
||||||
|
$tables['missing'][$table] = $this->__columns($Object);
|
||||||
|
$tables['missing'][$table]['indexes'] = $db->index($Object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ksort($tables);
|
ksort($tables);
|
||||||
return compact('name', 'tables');
|
return compact('name', 'tables');
|
||||||
}
|
}
|
||||||
|
@ -249,7 +256,7 @@ class CakeSchema extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($tables as $table => $fields) {
|
foreach ($tables as $table => $fields) {
|
||||||
if(!is_numeric($table)) {
|
if(!is_numeric($table) && $table !== 'missing') {
|
||||||
$out .= "\tvar \${$table} = array(\n";
|
$out .= "\tvar \${$table} = array(\n";
|
||||||
if (is_array($fields)) {
|
if (is_array($fields)) {
|
||||||
$cols = array();
|
$cols = array();
|
||||||
|
@ -317,6 +324,9 @@ class CakeSchema extends Object {
|
||||||
}
|
}
|
||||||
$tables = array();
|
$tables = array();
|
||||||
foreach ($new as $table => $fields) {
|
foreach ($new as $table => $fields) {
|
||||||
|
if($table == 'missing') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (!array_key_exists($table, $old)) {
|
if (!array_key_exists($table, $old)) {
|
||||||
$tables[$table]['add'] = $fields;
|
$tables[$table]['add'] = $fields;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -38,8 +38,20 @@ class MyAppSchema extends CakeSchema {
|
||||||
|
|
||||||
var $connection = 'test_suite';
|
var $connection = 'test_suite';
|
||||||
|
|
||||||
|
var $comments = array(
|
||||||
|
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
||||||
|
'post_id' => array('type'=>'integer', 'null' => false),
|
||||||
|
'user_id' => array('type'=>'integer', 'null' => false),
|
||||||
|
'title' => array('type'=>'string', 'null' => false, 'length' => 100),
|
||||||
|
'comment' => array('type'=>'text', 'null' => false),
|
||||||
|
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||||
|
'created' => array('type'=>'datetime', 'null' => true),
|
||||||
|
'updated' => array('type'=>'datetime', 'null' => true),
|
||||||
|
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
||||||
|
);
|
||||||
|
|
||||||
var $posts = array(
|
var $posts = array(
|
||||||
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
||||||
'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
||||||
'title' => array('type'=>'string', 'null' => false, 'default' => 'Title'),
|
'title' => array('type'=>'string', 'null' => false, 'default' => 'Title'),
|
||||||
'summary' => array('type'=>'text', 'null' => true),
|
'summary' => array('type'=>'text', 'null' => true),
|
||||||
|
@ -51,18 +63,6 @@ class MyAppSchema extends CakeSchema {
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
var $comments = array(
|
|
||||||
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
|
||||||
'post_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
|
||||||
'user_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
|
||||||
'title' => array('type'=>'string', 'null' => false, 'length' => 100),
|
|
||||||
'comment' => array('type'=>'text', 'null' => false),
|
|
||||||
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
|
||||||
'created' => array('type'=>'datetime', 'null' => true),
|
|
||||||
'updated' => array('type'=>'datetime', 'null' => true),
|
|
||||||
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
|
||||||
);
|
|
||||||
|
|
||||||
function setup($version) {
|
function setup($version) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,24 +73,39 @@ class TestAppSchema extends CakeSchema {
|
||||||
|
|
||||||
var $name = 'MyApp';
|
var $name = 'MyApp';
|
||||||
|
|
||||||
|
var $comments = array(
|
||||||
|
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
||||||
|
'article_id' => array('type'=>'integer', 'null' => false),
|
||||||
|
'user_id' => array('type'=>'integer', 'null' => false),
|
||||||
|
'comment' => array('type'=>'text', 'null' => true),
|
||||||
|
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||||
|
'created' => array('type'=>'datetime', 'null' => true),
|
||||||
|
'updated' => array('type'=>'datetime', 'null' => true),
|
||||||
|
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
||||||
|
);
|
||||||
|
|
||||||
var $posts = array(
|
var $posts = array(
|
||||||
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
||||||
'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
'author_id' => array('type'=>'integer', 'null' => false),
|
||||||
'title' => array('type'=>'string', 'null' => false, 'default' => ''),
|
'title' => array('type'=>'string', 'null' => false),
|
||||||
'body' => array('type'=>'text', 'null' => true),
|
'body' => array('type'=>'text', 'null' => true),
|
||||||
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
||||||
'created' => array('type'=>'datetime', 'null' => true),
|
'created' => array('type'=>'datetime', 'null' => true),
|
||||||
'updated' => array('type'=>'datetime', 'null' => true),
|
'updated' => array('type'=>'datetime', 'null' => true),
|
||||||
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
||||||
);
|
);
|
||||||
var $comments = array(
|
|
||||||
'id' => array('type'=>'integer', 'null' => false, 'default' => null, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
var $posts_tags = array(
|
||||||
'article_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
|
||||||
'user_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
|
'tag_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
|
||||||
'comment' => array('type'=>'text', 'null' => true),
|
'indexes' => array('UNIQUE_TAG' => array('column'=> array('post_id', 'tag_id'), 'unique'=>1))
|
||||||
'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
);
|
||||||
'created' => array('type'=>'datetime', 'null' => true),
|
|
||||||
'updated' => array('type'=>'datetime', 'null' => true),
|
var $tags = array(
|
||||||
|
'id' => array('type' => 'integer', 'null'=> false, 'key' => 'primary', 'extra'=> 'auto_increment'),
|
||||||
|
'tag' => array('type' => 'string', 'null' => false),
|
||||||
|
'created' => array('type' => 'datetime', 'null' => true),
|
||||||
|
'updated' => array('type' => 'datetime', 'null' => true),
|
||||||
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -109,8 +124,10 @@ class TestAppSchema extends CakeSchema {
|
||||||
*/
|
*/
|
||||||
class SchemaPost extends CakeTestModel {
|
class SchemaPost extends CakeTestModel {
|
||||||
var $name = 'SchemaPost';
|
var $name = 'SchemaPost';
|
||||||
//var $useTable = 'posts';
|
var $useTable = 'posts';
|
||||||
var $hasMany = array('SchemaComment');
|
var $hasMany = array('SchemaComment');
|
||||||
|
var $hasAndBelongsToMany = array('SchemaTag');
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Short description for class.
|
* Short description for class.
|
||||||
|
@ -120,9 +137,20 @@ class SchemaPost extends CakeTestModel {
|
||||||
*/
|
*/
|
||||||
class SchemaComment extends CakeTestModel {
|
class SchemaComment extends CakeTestModel {
|
||||||
var $name = 'SchemaComment';
|
var $name = 'SchemaComment';
|
||||||
//var $useTable = 'comments';
|
var $useTable = 'comments';
|
||||||
var $belongsTo = array('SchemaPost');
|
var $belongsTo = array('SchemaPost');
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Short description for class.
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.cases.libs.model
|
||||||
|
*/
|
||||||
|
class SchemaTag extends CakeTestModel {
|
||||||
|
var $name = 'SchemaTag';
|
||||||
|
var $useTable = 'tags';
|
||||||
|
var $hasAndBelongsToMany = array('SchemaPost');
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Short description for class.
|
* Short description for class.
|
||||||
*
|
*
|
||||||
|
@ -131,25 +159,29 @@ class SchemaComment extends CakeTestModel {
|
||||||
*/
|
*/
|
||||||
class CakeSchemaTest extends CakeTestCase {
|
class CakeSchemaTest extends CakeTestCase {
|
||||||
|
|
||||||
var $fixtures = array('core.post', 'core.comment', 'core.author');
|
var $fixtures = array('core.post', 'core.comment', 'core.author', 'core.tag', 'core.posts_tag');
|
||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
$this->Schema = new TestAppSchema();
|
$this->Schema = new TestAppSchema();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function testSchemaGeneration() {
|
function testSchemaRead() {
|
||||||
|
$read = $this->Schema->read(array('connection'=>'test_suite', 'name'=>'TestApp', 'models'=>array('SchemaPost', 'SchemaComment', 'SchemaTag')));
|
||||||
$read = $this->Schema->read(array('connection'=>'test_suite', 'name'=>'TestApp', 'models'=>array('post', 'comment')));
|
unset($read['tables']['missing']);
|
||||||
$this->assertEqual($read['tables'], $this->Schema->tables);
|
$this->assertEqual($read['tables'], $this->Schema->tables);
|
||||||
|
}
|
||||||
|
|
||||||
$write = $this->Schema->write(array('name'=>'MyOtherApp', 'tables'=> $read['tables'], 'path'=> TMP . 'tests'));
|
function testSchemaWrite() {
|
||||||
|
|
||||||
|
$write = $this->Schema->write(array('name'=>'MyOtherApp', 'tables'=> $this->Schema->tables, 'path'=> TMP . 'tests'));
|
||||||
$file = file_get_contents(TMP . 'tests' . DS .'schema.php');
|
$file = file_get_contents(TMP . 'tests' . DS .'schema.php');
|
||||||
$this->assertEqual($write, $file);
|
$this->assertEqual($write, $file);
|
||||||
|
|
||||||
require_once( TMP . 'tests' . DS .'schema.php');
|
require_once( TMP . 'tests' . DS .'schema.php');
|
||||||
$OtherSchema = new MyOtherAppSchema();
|
$OtherSchema = new MyOtherAppSchema();
|
||||||
$this->assertEqual($read['tables'], $OtherSchema->tables);
|
$this->assertEqual($this->Schema->tables, $OtherSchema->tables);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSchemaComparison() {
|
function testSchemaComparison() {
|
||||||
|
@ -161,8 +193,8 @@ class CakeSchemaTest extends CakeTestCase {
|
||||||
'change'=> array('title'=>array('type'=>'string', 'null'=> false, 'default'=> 'Title'), 'published'=>array('type'=>'string', 'null'=> true, 'default'=>'Y', 'length'=> '1')),
|
'change'=> array('title'=>array('type'=>'string', 'null'=> false, 'default'=> 'Title'), 'published'=>array('type'=>'string', 'null'=> true, 'default'=>'Y', 'length'=> '1')),
|
||||||
),
|
),
|
||||||
'comments'=> array(
|
'comments'=> array(
|
||||||
'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false, 'default'=>''), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
|
'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
|
||||||
'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false, 'default'=>'')),
|
'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false)),
|
||||||
'change'=>array('comment'=>array('type'=>'text', 'null'=> false))
|
'change'=>array('comment'=>array('type'=>'text', 'null'=> false))
|
||||||
|
|
||||||
),
|
),
|
||||||
|
|
50
cake/tests/fixtures/posts_tag_fixture.php
vendored
Normal file
50
cake/tests/fixtures/posts_tag_fixture.php
vendored
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
/* SVN FILE: $Id$ */
|
||||||
|
/**
|
||||||
|
* Short description for file.
|
||||||
|
*
|
||||||
|
* Long description for file
|
||||||
|
*
|
||||||
|
* PHP versions 4 and 5
|
||||||
|
*
|
||||||
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
||||||
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
||||||
|
* 1785 E. Sahara Avenue, Suite 490-204
|
||||||
|
* Las Vegas, Nevada 89104
|
||||||
|
*
|
||||||
|
* Licensed under The Open Group Test Suite License
|
||||||
|
* Redistributions of files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @filesource
|
||||||
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
||||||
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.fixtures
|
||||||
|
* @since CakePHP(tm) v 1.2.0.4667
|
||||||
|
* @version $Revision$
|
||||||
|
* @modifiedby $LastChangedBy$
|
||||||
|
* @lastmodified $Date$
|
||||||
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Short description for class.
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.fixtures
|
||||||
|
*/
|
||||||
|
class PostsTagFixture extends CakeTestFixture {
|
||||||
|
var $name = 'PostsTag';
|
||||||
|
var $fields = array(
|
||||||
|
'post_id' => array('type' => 'integer', 'null' => false),
|
||||||
|
'tag_id' => array('type' => 'integer', 'null' => false),
|
||||||
|
'indexes' => array('UNIQUE_TAG' => array('column'=> array('post_id', 'tag_id'), 'unique'=> 1))
|
||||||
|
);
|
||||||
|
var $records = array(
|
||||||
|
array('post_id' => 1, 'tag_id' => 1),
|
||||||
|
array('post_id' => 1, 'tag_id' => 2),
|
||||||
|
array('post_id' => 2, 'tag_id' => 1),
|
||||||
|
array('post_id' => 2, 'tag_id' => 3)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Reference in a new issue