/src/Propel/Runtime/Map/DatabaseMap.php

https://github.com/fzaninotto/Propel2 · PHP · 211 lines · 87 code · 29 blank · 95 comment · 9 complexity · d9812cd12d5151236403d89cc539e3bc MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\Runtime\Map;
  10. use Propel\Runtime\Map\Exception\TableNotFoundException;
  11. use Propel\Runtime\Propel;
  12. /**
  13. * DatabaseMap is used to model a database.
  14. *
  15. * GENERAL NOTE
  16. * ------------
  17. * The propel.map classes are abstract building-block classes for modeling
  18. * the database at runtime. These classes are similar (a lite version) to the
  19. * propel.engine.database.model classes, which are build-time modeling classes.
  20. * These classes in themselves do not do any database metadata lookups.
  21. *
  22. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  23. * @author John D. McNally <jmcnally@collab.net> (Torque)
  24. * @author Daniel Rall <dlr@collab.net> (Torque)
  25. */
  26. class DatabaseMap
  27. {
  28. /**
  29. * Name of the database.
  30. *
  31. * @var string
  32. */
  33. protected $name;
  34. /**
  35. * Tables in the database, using table name as key
  36. *
  37. * @var TableMap[]
  38. */
  39. protected $tables = array();
  40. /**
  41. * Tables in the database, using table phpName as key
  42. *
  43. * @var TableMap[]
  44. */
  45. protected $tablesByPhpName = array();
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $name Name of the database.
  50. */
  51. public function __construct($name)
  52. {
  53. $this->name = $name;
  54. }
  55. /**
  56. * Get the name of this database.
  57. *
  58. * @return string The name of the database.
  59. */
  60. public function getName()
  61. {
  62. return $this->name;
  63. }
  64. /**
  65. * Add a new table to the database by name.
  66. *
  67. * @param string $tableName The name of the table.
  68. * @return \Propel\Runtime\Map\TableMap The newly created TableMap.
  69. */
  70. public function addTable($tableName)
  71. {
  72. $this->tables[$tableName] = new TableMap($tableName, $this);
  73. return $this->tables[$tableName];
  74. }
  75. /**
  76. * Add a new table object to the database.
  77. *
  78. * @param \Propel\Runtime\Map\TableMap $table The table to add
  79. */
  80. public function addTableObject(TableMap $table)
  81. {
  82. $table->setDatabaseMap($this);
  83. $this->tables[$table->getName()] = $table;
  84. $this->tablesByPhpName[$table->getClassName()] = $table;
  85. }
  86. /**
  87. * Add a new table to the database, using the tablemap class name.
  88. *
  89. * @param string $tableMapClass The name of the table map to add
  90. * @return \Propel\Runtime\Map\TableMap The TableMap object
  91. */
  92. public function addTableFromMapClass($tableMapClass)
  93. {
  94. $table = new $tableMapClass();
  95. if (!$this->hasTable($table->getName())) {
  96. $this->addTableObject($table);
  97. return $table;
  98. }
  99. return $this->getTable($table->getName());
  100. }
  101. /**
  102. * Does this database contain this specific table?
  103. *
  104. * @param string $name The String representation of the table.
  105. * @return boolean True if the database contains the table.
  106. */
  107. public function hasTable($name)
  108. {
  109. if (strpos($name, '.') > 0) {
  110. $name = substr($name, 0, strpos($name, '.'));
  111. }
  112. return isset($this->tables[$name]);
  113. }
  114. /**
  115. * Get a TableMap for the table by name.
  116. *
  117. * @param string $name Name of the table.
  118. * @return \Propel\Runtime\Map\TableMap A TableMap
  119. * @throws \Propel\Runtime\Map\Exception\TableNotFoundException If the table is undefined
  120. */
  121. public function getTable($name)
  122. {
  123. if (!isset($this->tables[$name])) {
  124. throw new TableNotFoundException(sprintf('Cannot fetch TableMap for undefined table: %s.', $name));
  125. }
  126. return $this->tables[$name];
  127. }
  128. /**
  129. * Get a TableMap[] of all of the tables in the database.
  130. *
  131. * @return TableMap[]
  132. */
  133. public function getTables()
  134. {
  135. return $this->tables;
  136. }
  137. /**
  138. * Get a ColumnMap for the column by name.
  139. * Name must be fully qualified, e.g. book.AUTHOR_ID
  140. *
  141. * @param $qualifiedColumnName Name of the column.
  142. * @return \Propel\Runtime\Map\ColumnMap A TableMap
  143. * @throws \Propel\Runtime\Map\TableNotFoundException If the table is undefined, or if the table is undefined
  144. */
  145. public function getColumn($qualifiedColumnName)
  146. {
  147. list($tableName, $columnName) = explode('.', $qualifiedColumnName);
  148. return $this->getTable($tableName)->getColumn($columnName, false);
  149. }
  150. public function getTableByPhpName($phpName)
  151. {
  152. if (isset($this->tablesByPhpName[$phpName])) {
  153. return $this->tablesByPhpName[$phpName];
  154. }
  155. if (class_exists($tmClass = $phpName . 'TableMap')) {
  156. $this->addTableFromMapClass($tmClass);
  157. return $this->tablesByPhpName[$phpName];
  158. }
  159. if (class_exists($tmClass = substr_replace($phpName, '\\Map\\', strrpos($phpName, '\\'), 1) . 'TableMap')
  160. || class_exists($tmClass = '\\Map\\' .$phpName . 'TableMap')) {
  161. $this->addTableFromMapClass($tmClass);
  162. if (isset($this->tablesByPhpName[$phpName])) {
  163. return $this->tablesByPhpName[$phpName];
  164. }
  165. $phpName = '\\'.$phpName;
  166. if (isset($this->tablesByPhpName[$phpName])) {
  167. return $this->tablesByPhpName[$phpName];
  168. }
  169. }
  170. throw new TableNotFoundException(sprintf('Cannot fetch TableMap for undefined table phpName: %s.', $phpName));
  171. }
  172. /**
  173. * Convenience method to get the AdapterInterface registered with Propel for this database.
  174. * @see Propel::getServiceContainer()->getAdapter(string) .
  175. *
  176. * @return \Propel\Runtime\Adapter\AdapterInterface
  177. */
  178. public function getAbstractAdapter()
  179. {
  180. return Propel::getServiceContainer()->getAdapter($this->name);
  181. }
  182. }