DatabaseMap.php 5.2 KB

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