Table.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Types\Type;
  21. use Doctrine\DBAL\Schema\Visitor\Visitor;
  22. use Doctrine\DBAL\DBALException;
  23. /**
  24. * Object Representation of a table.
  25. *
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. */
  30. class Table extends AbstractAsset
  31. {
  32. /**
  33. * @var string
  34. */
  35. protected $_name = null;
  36. /**
  37. * @var \Doctrine\DBAL\Schema\Column[]
  38. */
  39. protected $_columns = array();
  40. /**
  41. * @var \Doctrine\DBAL\Schema\Index[]
  42. */
  43. protected $_indexes = array();
  44. /**
  45. * @var string
  46. */
  47. protected $_primaryKeyName = false;
  48. /**
  49. * @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
  50. */
  51. protected $_fkConstraints = array();
  52. /**
  53. * @var array
  54. */
  55. protected $_options = array();
  56. /**
  57. * @var \Doctrine\DBAL\Schema\SchemaConfig
  58. */
  59. protected $_schemaConfig = null;
  60. /**
  61. * @param string $tableName
  62. * @param array $columns
  63. * @param array $indexes
  64. * @param array $fkConstraints
  65. * @param integer $idGeneratorType
  66. * @param array $options
  67. *
  68. * @throws \Doctrine\DBAL\DBALException
  69. */
  70. public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType = 0, array $options=array())
  71. {
  72. if (strlen($tableName) == 0) {
  73. throw DBALException::invalidTableName($tableName);
  74. }
  75. $this->_setName($tableName);
  76. $this->_idGeneratorType = $idGeneratorType;
  77. foreach ($columns as $column) {
  78. $this->_addColumn($column);
  79. }
  80. foreach ($indexes as $idx) {
  81. $this->_addIndex($idx);
  82. }
  83. foreach ($fkConstraints as $constraint) {
  84. $this->_addForeignKeyConstraint($constraint);
  85. }
  86. $this->_options = $options;
  87. }
  88. /**
  89. * @param \Doctrine\DBAL\Schema\SchemaConfig $schemaConfig
  90. *
  91. * @return void
  92. */
  93. public function setSchemaConfig(SchemaConfig $schemaConfig)
  94. {
  95. $this->_schemaConfig = $schemaConfig;
  96. }
  97. /**
  98. * @return integer
  99. */
  100. protected function _getMaxIdentifierLength()
  101. {
  102. if ($this->_schemaConfig instanceof SchemaConfig) {
  103. return $this->_schemaConfig->getMaxIdentifierLength();
  104. } else {
  105. return 63;
  106. }
  107. }
  108. /**
  109. * Sets the Primary Key.
  110. *
  111. * @param array $columns
  112. * @param string|boolean $indexName
  113. *
  114. * @return \Doctrine\DBAL\Schema\Table
  115. */
  116. public function setPrimaryKey(array $columns, $indexName = false)
  117. {
  118. $primaryKey = $this->_createIndex($columns, $indexName ?: "primary", true, true);
  119. foreach ($columns as $columnName) {
  120. $column = $this->getColumn($columnName);
  121. $column->setNotnull(true);
  122. }
  123. return $primaryKey;
  124. }
  125. /**
  126. * @param array $columnNames
  127. * @param string|null $indexName
  128. * @param array $flags
  129. *
  130. * @return \Doctrine\DBAL\Schema\Table
  131. */
  132. public function addIndex(array $columnNames, $indexName = null, array $flags = array())
  133. {
  134. if($indexName == null) {
  135. $indexName = $this->_generateIdentifierName(
  136. array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
  137. );
  138. }
  139. return $this->_createIndex($columnNames, $indexName, false, false, $flags);
  140. }
  141. /**
  142. * Drops the primary key from this table.
  143. *
  144. * @return void
  145. */
  146. public function dropPrimaryKey()
  147. {
  148. $this->dropIndex($this->_primaryKeyName);
  149. $this->_primaryKeyName = false;
  150. }
  151. /**
  152. * Drops an index from this table.
  153. *
  154. * @param string $indexName The index name.
  155. *
  156. * @return void
  157. *
  158. * @throws \Doctrine\DBAL\Schema\SchemaException If the index does not exist.
  159. */
  160. public function dropIndex($indexName)
  161. {
  162. $indexName = strtolower($indexName);
  163. if ( ! $this->hasIndex($indexName)) {
  164. throw SchemaException::indexDoesNotExist($indexName, $this->_name);
  165. }
  166. unset($this->_indexes[$indexName]);
  167. }
  168. /**
  169. * @param array $columnNames
  170. * @param string|null $indexName
  171. *
  172. * @return \Doctrine\DBAL\Schema\Table
  173. */
  174. public function addUniqueIndex(array $columnNames, $indexName = null)
  175. {
  176. if ($indexName === null) {
  177. $indexName = $this->_generateIdentifierName(
  178. array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
  179. );
  180. }
  181. return $this->_createIndex($columnNames, $indexName, true, false);
  182. }
  183. /**
  184. * Checks if an index begins in the order of the given columns.
  185. *
  186. * @param array $columnsNames
  187. *
  188. * @return boolean
  189. */
  190. public function columnsAreIndexed(array $columnsNames)
  191. {
  192. foreach ($this->getIndexes() as $index) {
  193. /* @var $index Index */
  194. if ($index->spansColumns($columnsNames)) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. /**
  201. * @param array $columnNames
  202. * @param string $indexName
  203. * @param boolean $isUnique
  204. * @param boolean $isPrimary
  205. * @param array $flags
  206. *
  207. * @return \Doctrine\DBAL\Schema\Table
  208. *
  209. * @throws \Doctrine\DBAL\Schema\SchemaException
  210. */
  211. private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array())
  212. {
  213. if (preg_match('(([^a-zA-Z0-9_]+))', $indexName)) {
  214. throw SchemaException::indexNameInvalid($indexName);
  215. }
  216. foreach ($columnNames as $columnName => $indexColOptions) {
  217. if (is_numeric($columnName) && is_string($indexColOptions)) {
  218. $columnName = $indexColOptions;
  219. }
  220. if ( ! $this->hasColumn($columnName)) {
  221. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  222. }
  223. }
  224. $this->_addIndex(new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags));
  225. return $this;
  226. }
  227. /**
  228. * @param string $columnName
  229. * @param string $typeName
  230. * @param array $options
  231. *
  232. * @return \Doctrine\DBAL\Schema\Column
  233. */
  234. public function addColumn($columnName, $typeName, array $options=array())
  235. {
  236. $column = new Column($columnName, Type::getType($typeName), $options);
  237. $this->_addColumn($column);
  238. return $column;
  239. }
  240. /**
  241. * Renames a Column.
  242. *
  243. * @param string $oldColumnName
  244. * @param string $newColumnName
  245. *
  246. * @return \Doctrine\DBAL\Schema\Table
  247. *
  248. * @throws \Doctrine\DBAL\DBALException
  249. */
  250. public function renameColumn($oldColumnName, $newColumnName)
  251. {
  252. throw new DBALException("Table#renameColumn() was removed, because it drops and recreates " .
  253. "the column instead. There is no fix available, because a schema diff cannot reliably detect if a " .
  254. "column was renamed or one column was created and another one dropped.");
  255. }
  256. /**
  257. * Change Column Details.
  258. *
  259. * @param string $columnName
  260. * @param array $options
  261. *
  262. * @return \Doctrine\DBAL\Schema\Table
  263. */
  264. public function changeColumn($columnName, array $options)
  265. {
  266. $column = $this->getColumn($columnName);
  267. $column->setOptions($options);
  268. return $this;
  269. }
  270. /**
  271. * Drops a Column from the Table.
  272. *
  273. * @param string $columnName
  274. *
  275. * @return \Doctrine\DBAL\Schema\Table
  276. */
  277. public function dropColumn($columnName)
  278. {
  279. $columnName = strtolower($columnName);
  280. unset($this->_columns[$columnName]);
  281. return $this;
  282. }
  283. /**
  284. * Adds a foreign key constraint.
  285. *
  286. * Name is inferred from the local columns.
  287. *
  288. * @param \Doctrine\DBAL\Schema\Table $foreignTable
  289. * @param array $localColumnNames
  290. * @param array $foreignColumnNames
  291. * @param array $options
  292. * @param string|null $constraintName
  293. *
  294. * @return \Doctrine\DBAL\Schema\Table
  295. */
  296. public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null)
  297. {
  298. $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
  299. return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
  300. }
  301. /**
  302. * Adds a foreign key constraint.
  303. *
  304. * Name is to be generated by the database itself.
  305. *
  306. * @deprecated Use {@link addForeignKeyConstraint}
  307. *
  308. * @param \Doctrine\DBAL\Schema\Table $foreignTable
  309. * @param array $localColumnNames
  310. * @param array $foreignColumnNames
  311. * @param array $options
  312. *
  313. * @return \Doctrine\DBAL\Schema\Table
  314. */
  315. public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  316. {
  317. return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
  318. }
  319. /**
  320. * Adds a foreign key constraint with a given name.
  321. *
  322. * @deprecated Use {@link addForeignKeyConstraint}
  323. *
  324. * @param string $name
  325. * @param \Doctrine\DBAL\Schema\Table $foreignTable
  326. * @param array $localColumnNames
  327. * @param array $foreignColumnNames
  328. * @param array $options
  329. *
  330. * @return \Doctrine\DBAL\Schema\Table
  331. *
  332. * @throws \Doctrine\DBAL\Schema\SchemaException
  333. */
  334. public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  335. {
  336. if ($foreignTable instanceof Table) {
  337. foreach ($foreignColumnNames as $columnName) {
  338. if ( ! $foreignTable->hasColumn($columnName)) {
  339. throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
  340. }
  341. }
  342. }
  343. foreach ($localColumnNames as $columnName) {
  344. if ( ! $this->hasColumn($columnName)) {
  345. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  346. }
  347. }
  348. $constraint = new ForeignKeyConstraint(
  349. $localColumnNames, $foreignTable, $foreignColumnNames, $name, $options
  350. );
  351. $this->_addForeignKeyConstraint($constraint);
  352. return $this;
  353. }
  354. /**
  355. * @param string $name
  356. * @param string $value
  357. *
  358. * @return \Doctrine\DBAL\Schema\Table
  359. */
  360. public function addOption($name, $value)
  361. {
  362. $this->_options[$name] = $value;
  363. return $this;
  364. }
  365. /**
  366. * @param \Doctrine\DBAL\Schema\Column $column
  367. *
  368. * @return void
  369. *
  370. * @throws \Doctrine\DBAL\Schema\SchemaException
  371. */
  372. protected function _addColumn(Column $column)
  373. {
  374. $columnName = $column->getName();
  375. $columnName = strtolower($columnName);
  376. if (isset($this->_columns[$columnName])) {
  377. throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
  378. }
  379. $this->_columns[$columnName] = $column;
  380. }
  381. /**
  382. * Adds an index to the table.
  383. *
  384. * @param \Doctrine\DBAL\Schema\Index $indexCandidate
  385. *
  386. * @return \Doctrine\DBAL\Schema\Table
  387. *
  388. * @throws \Doctrine\DBAL\Schema\SchemaException
  389. */
  390. protected function _addIndex(Index $indexCandidate)
  391. {
  392. // check for duplicates
  393. foreach ($this->_indexes as $existingIndex) {
  394. if ($indexCandidate->isFullfilledBy($existingIndex)) {
  395. return $this;
  396. }
  397. }
  398. $indexName = $indexCandidate->getName();
  399. $indexName = strtolower($indexName);
  400. if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
  401. throw SchemaException::indexAlreadyExists($indexName, $this->_name);
  402. }
  403. // remove overruled indexes
  404. foreach ($this->_indexes as $idxKey => $existingIndex) {
  405. if ($indexCandidate->overrules($existingIndex)) {
  406. unset($this->_indexes[$idxKey]);
  407. }
  408. }
  409. if ($indexCandidate->isPrimary()) {
  410. $this->_primaryKeyName = $indexName;
  411. }
  412. $this->_indexes[$indexName] = $indexCandidate;
  413. return $this;
  414. }
  415. /**
  416. * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $constraint
  417. *
  418. * @return void
  419. */
  420. protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
  421. {
  422. $constraint->setLocalTable($this);
  423. if(strlen($constraint->getName())) {
  424. $name = $constraint->getName();
  425. } else {
  426. $name = $this->_generateIdentifierName(
  427. array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
  428. );
  429. }
  430. $name = strtolower($name);
  431. $this->_fkConstraints[$name] = $constraint;
  432. // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
  433. // In the case of __construct calling this method during hydration from schema-details all the explicitly added indexes
  434. // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
  435. $this->addIndex($constraint->getColumns());
  436. }
  437. /**
  438. * Returns whether this table has a foreign key constraint with the given name.
  439. *
  440. * @param string $constraintName
  441. *
  442. * @return boolean
  443. */
  444. public function hasForeignKey($constraintName)
  445. {
  446. $constraintName = strtolower($constraintName);
  447. return isset($this->_fkConstraints[$constraintName]);
  448. }
  449. /**
  450. * Returns the foreign key constraint with the given name.
  451. *
  452. * @param string $constraintName The constraint name.
  453. *
  454. * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint
  455. *
  456. * @throws \Doctrine\DBAL\Schema\SchemaException If the foreign key does not exist.
  457. */
  458. public function getForeignKey($constraintName)
  459. {
  460. $constraintName = strtolower($constraintName);
  461. if(!$this->hasForeignKey($constraintName)) {
  462. throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
  463. }
  464. return $this->_fkConstraints[$constraintName];
  465. }
  466. /**
  467. * Removes the foreign key constraint with the given name.
  468. *
  469. * @param string $constraintName The constraint name.
  470. *
  471. * @return void
  472. *
  473. * @throws \Doctrine\DBAL\Schema\SchemaException
  474. */
  475. public function removeForeignKey($constraintName)
  476. {
  477. $constraintName = strtolower($constraintName);
  478. if(!$this->hasForeignKey($constraintName)) {
  479. throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
  480. }
  481. unset($this->_fkConstraints[$constraintName]);
  482. }
  483. /**
  484. * @return \Doctrine\DBAL\Schema\Column[]
  485. */
  486. public function getColumns()
  487. {
  488. $columns = $this->_columns;
  489. $pkCols = array();
  490. $fkCols = array();
  491. if ($this->hasPrimaryKey()) {
  492. $pkCols = $this->getPrimaryKey()->getColumns();
  493. }
  494. foreach ($this->getForeignKeys() as $fk) {
  495. /* @var $fk ForeignKeyConstraint */
  496. $fkCols = array_merge($fkCols, $fk->getColumns());
  497. }
  498. $colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));
  499. uksort($columns, function($a, $b) use($colNames) {
  500. return (array_search($a, $colNames) >= array_search($b, $colNames));
  501. });
  502. return $columns;
  503. }
  504. /**
  505. * Returns whether this table has a Column with the given name.
  506. *
  507. * @param string $columnName The column name.
  508. *
  509. * @return boolean
  510. */
  511. public function hasColumn($columnName)
  512. {
  513. $columnName = $this->trimQuotes(strtolower($columnName));
  514. return isset($this->_columns[$columnName]);
  515. }
  516. /**
  517. * Returns the Column with the given name.
  518. *
  519. * @param string $columnName The column name.
  520. *
  521. * @return \Doctrine\DBAL\Schema\Column
  522. *
  523. * @throws \Doctrine\DBAL\Schema\SchemaException If the column does not exist.
  524. */
  525. public function getColumn($columnName)
  526. {
  527. $columnName = strtolower($this->trimQuotes($columnName));
  528. if ( ! $this->hasColumn($columnName)) {
  529. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  530. }
  531. return $this->_columns[$columnName];
  532. }
  533. /**
  534. * Returns the primary key.
  535. *
  536. * @return \Doctrine\DBAL\Schema\Index|null The primary key, or null if this Table has no primary key.
  537. */
  538. public function getPrimaryKey()
  539. {
  540. if ( ! $this->hasPrimaryKey()) {
  541. return null;
  542. }
  543. return $this->getIndex($this->_primaryKeyName);
  544. }
  545. /**
  546. * Returns the primary key columns.
  547. *
  548. * @return array
  549. *
  550. * @throws \Doctrine\DBAL\DBALException
  551. */
  552. public function getPrimaryKeyColumns()
  553. {
  554. if ( ! $this->hasPrimaryKey()) {
  555. throw new DBALException("Table " . $this->getName() . " has no primary key.");
  556. }
  557. return $this->getPrimaryKey()->getColumns();
  558. }
  559. /**
  560. * Returns whether this table has a primary key.
  561. *
  562. * @return boolean
  563. */
  564. public function hasPrimaryKey()
  565. {
  566. return ($this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName));
  567. }
  568. /**
  569. * Returns whether this table has an Index with the given name.
  570. *
  571. * @param string $indexName The index name.
  572. *
  573. * @return boolean
  574. */
  575. public function hasIndex($indexName)
  576. {
  577. $indexName = strtolower($indexName);
  578. return (isset($this->_indexes[$indexName]));
  579. }
  580. /**
  581. * Returns the Index with the given name.
  582. *
  583. * @param string $indexName The index name.
  584. *
  585. * @return \Doctrine\DBAL\Schema\Index
  586. *
  587. * @throws \Doctrine\DBAL\Schema\SchemaException If the index does not exist.
  588. */
  589. public function getIndex($indexName)
  590. {
  591. $indexName = strtolower($indexName);
  592. if ( ! $this->hasIndex($indexName)) {
  593. throw SchemaException::indexDoesNotExist($indexName, $this->_name);
  594. }
  595. return $this->_indexes[$indexName];
  596. }
  597. /**
  598. * @return \Doctrine\DBAL\Schema\Index[]
  599. */
  600. public function getIndexes()
  601. {
  602. return $this->_indexes;
  603. }
  604. /**
  605. * Returns the foreign key constraints.
  606. *
  607. * @return \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
  608. */
  609. public function getForeignKeys()
  610. {
  611. return $this->_fkConstraints;
  612. }
  613. /**
  614. * @param string $name
  615. *
  616. * @return boolean
  617. */
  618. public function hasOption($name)
  619. {
  620. return isset($this->_options[$name]);
  621. }
  622. /**
  623. * @param string $name
  624. *
  625. * @return mixed
  626. */
  627. public function getOption($name)
  628. {
  629. return $this->_options[$name];
  630. }
  631. /**
  632. * @return array
  633. */
  634. public function getOptions()
  635. {
  636. return $this->_options;
  637. }
  638. /**
  639. * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
  640. *
  641. * @return void
  642. */
  643. public function visit(Visitor $visitor)
  644. {
  645. $visitor->acceptTable($this);
  646. foreach ($this->getColumns() as $column) {
  647. $visitor->acceptColumn($this, $column);
  648. }
  649. foreach ($this->getIndexes() as $index) {
  650. $visitor->acceptIndex($this, $index);
  651. }
  652. foreach ($this->getForeignKeys() as $constraint) {
  653. $visitor->acceptForeignKey($this, $constraint);
  654. }
  655. }
  656. /**
  657. * Clone of a Table triggers a deep clone of all affected assets.
  658. *
  659. * @return void
  660. */
  661. public function __clone()
  662. {
  663. foreach ($this->_columns as $k => $column) {
  664. $this->_columns[$k] = clone $column;
  665. }
  666. foreach ($this->_indexes as $k => $index) {
  667. $this->_indexes[$k] = clone $index;
  668. }
  669. foreach ($this->_fkConstraints as $k => $fk) {
  670. $this->_fkConstraints[$k] = clone $fk;
  671. $this->_fkConstraints[$k]->setLocalTable($this);
  672. }
  673. }
  674. }