diff --git a/lib/Doctrine/DBAL/Schema/Schema.php b/lib/Doctrine/DBAL/Schema/Schema.php index 510d55a84..96a70552b 100644 --- a/lib/Doctrine/DBAL/Schema/Schema.php +++ b/lib/Doctrine/DBAL/Schema/Schema.php @@ -309,4 +309,19 @@ class Schema extends AbstractAsset $sequence->visit($visitor); } } + + /** + * Cloning a Schema triggers a deep clone of all related assets. + * + * @return void + */ + public function __clone() + { + foreach ($this->_tables AS $k => $table) { + $this->_tables[$k] = clone $table; + } + foreach ($this->_sequences AS $k => $sequence) { + $this->_sequences[$k] = clone $sequence; + } + } } diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index c6e7eb5ca..18ab773b2 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -620,4 +620,21 @@ class Table extends AbstractAsset $visitor->acceptForeignKey($this, $constraint); } } + + /** + * Clone of a Table triggers a deep clone of all affected assets + */ + public function __clone() + { + foreach ($this->_columns AS $k => $column) { + $this->_columns[$k] = clone $column; + } + foreach ($this->_indexes AS $k => $index) { + $this->_indexes[$k] = clone $index; + } + foreach ($this->_fkConstraints AS $k => $fk) { + $this->_fkConstraints[$k] = clone $fk; + $this->_fkConstraints[$k]->setLocalTable($this); + } + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php b/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php index 4a424dad8..46648ef0d 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/SchemaTest.php @@ -212,4 +212,31 @@ class SchemaTest extends \PHPUnit_Framework_TestCase $this->assertTrue($table->hasIndex('le_id_idx')); } + + public function testDeepClone() + { + $schema = new Schema(); + $sequence = $schema->createSequence('baz'); + + $tableA = $schema->createTable('foo'); + $tableA->createColumn('id', 'integer'); + + $tableB = $schema->createTable('bar'); + $tableB->createColumn('id', 'integer'); + $tableB->createcolumn('foo_id', 'integer'); + $tableB->addForeignKeyConstraint($tableA, array('foo_id'), array('id')); + + $schemaNew = clone $schema; + + $this->assertNotSame($sequence, $schemaNew->getSequence('baz')); + + $this->assertNotSame($tableA, $schemaNew->getTable('foo')); + $this->assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id')); + + $this->assertNotSame($tableB, $schemaNew->getTable('bar')); + $this->assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id')); + + $fk = current( $schemaNew->getTable('bar')->getForeignKeys() ); + $this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable')); + } } \ No newline at end of file