RelationMap.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. * RelationMap is used to model a database relationship.
  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 Francois Zaninotto
  20. * @version $Revision: 1728 $
  21. * @package propel.runtime.map
  22. */
  23. class RelationMap
  24. {
  25. const
  26. // types
  27. MANY_TO_ONE = 1,
  28. ONE_TO_MANY = 2,
  29. ONE_TO_ONE = 3,
  30. MANY_TO_MANY = 4,
  31. // representations
  32. LOCAL_TO_FOREIGN = 0,
  33. LEFT_TO_RIGHT = 1;
  34. protected
  35. $name,
  36. $type,
  37. $localTable,
  38. $foreignTable,
  39. $localColumns = array(),
  40. $foreignColumns = array(),
  41. $onUpdate, $onDelete;
  42. /**
  43. * Constructor.
  44. *
  45. * @param string $name Name of the relation.
  46. */
  47. public function __construct($name)
  48. {
  49. $this->name = $name;
  50. }
  51. /**
  52. * Get the name of this relation.
  53. *
  54. * @return string The name of the relation.
  55. */
  56. public function getName()
  57. {
  58. return $this->name;
  59. }
  60. /**
  61. * Set the type
  62. *
  63. * @param integer $type The relation type (either self::MANY_TO_ONE, self::ONE_TO_MANY, or self::ONE_TO_ONE)
  64. */
  65. public function setType($type)
  66. {
  67. $this->type = $type;
  68. }
  69. /**
  70. * Get the type
  71. *
  72. * @return integer the relation type
  73. */
  74. public function getType()
  75. {
  76. return $this->type;
  77. }
  78. /**
  79. * Set the local table
  80. *
  81. * @param TableMap $table The local table for this relationship
  82. */
  83. public function setLocalTable($table)
  84. {
  85. $this->localTable = $table;
  86. }
  87. /**
  88. * Get the local table
  89. *
  90. * @return TableMap The local table for this relationship
  91. */
  92. public function getLocalTable()
  93. {
  94. return $this->localTable;
  95. }
  96. /**
  97. * Set the foreign table
  98. *
  99. * @param TableMap $table The foreign table for this relationship
  100. */
  101. public function setForeignTable($table)
  102. {
  103. $this->foreignTable = $table;
  104. }
  105. /**
  106. * Get the foreign table
  107. *
  108. * @return TableMap The foreign table for this relationship
  109. */
  110. public function getForeignTable()
  111. {
  112. return $this->foreignTable;
  113. }
  114. /**
  115. * Get the left table of the relation
  116. *
  117. * @return TableMap The left table for this relationship
  118. */
  119. public function getLeftTable()
  120. {
  121. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalTable() : $this->getForeignTable();
  122. }
  123. /**
  124. * Get the right table of the relation
  125. *
  126. * @return TableMap The right table for this relationship
  127. */
  128. public function getRightTable()
  129. {
  130. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignTable() : $this->getLocalTable();
  131. }
  132. /**
  133. * Add a column mapping
  134. *
  135. * @param ColumnMap $local The local column
  136. * @param ColumnMap $foreign The foreign column
  137. */
  138. public function addColumnMapping(ColumnMap $local, ColumnMap $foreign)
  139. {
  140. $this->localColumns[] = $local;
  141. $this->foreignColumns[] = $foreign;
  142. }
  143. /**
  144. * Get an associative array mapping local column names to foreign column names
  145. * The arrangement of the returned array depends on the $direction parameter:
  146. * - If the value is RelationMap::LOCAL_TO_FOREIGN, then the returned array is local => foreign
  147. * - If the value is RelationMap::LEFT_TO_RIGHT, then the returned array is left => right
  148. *
  149. * @param int $direction How the associative array must return columns
  150. * @return Array Associative array (local => foreign) of fully qualified column names
  151. */
  152. public function getColumnMappings($direction = RelationMap::LOCAL_TO_FOREIGN)
  153. {
  154. $h = array();
  155. if ($direction == RelationMap::LEFT_TO_RIGHT && $this->getType() == RelationMap::MANY_TO_ONE) {
  156. $direction = RelationMap::LOCAL_TO_FOREIGN;
  157. }
  158. for ($i=0, $size=count($this->localColumns); $i < $size; $i++) {
  159. if ($direction == RelationMap::LOCAL_TO_FOREIGN) {
  160. $h[$this->localColumns[$i]->getFullyQualifiedName()] = $this->foreignColumns[$i]->getFullyQualifiedName();
  161. } else {
  162. $h[$this->foreignColumns[$i]->getFullyQualifiedName()] = $this->localColumns[$i]->getFullyQualifiedName();
  163. }
  164. }
  165. return $h;
  166. }
  167. /**
  168. * Returns true if the relation has more than one column mapping
  169. *
  170. * @return boolean
  171. */
  172. public function isComposite()
  173. {
  174. return $this->countColumnMappings() > 1;
  175. }
  176. /**
  177. * Return the number of column mappings
  178. *
  179. * @return int
  180. */
  181. public function countColumnMappings()
  182. {
  183. return count($this->localColumns);
  184. }
  185. /**
  186. * Get the local columns
  187. *
  188. * @return Array list of ColumnMap objects
  189. */
  190. public function getLocalColumns()
  191. {
  192. return $this->localColumns;
  193. }
  194. /**
  195. * Get the foreign columns
  196. *
  197. * @return Array list of ColumnMap objects
  198. */
  199. public function getForeignColumns()
  200. {
  201. return $this->foreignColumns;
  202. }
  203. /**
  204. * Get the left columns of the relation
  205. *
  206. * @return array of ColumnMap objects
  207. */
  208. public function getLeftColumns()
  209. {
  210. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalColumns() : $this->getForeignColumns();
  211. }
  212. /**
  213. * Get the right columns of the relation
  214. *
  215. * @return array of ColumnMap objects
  216. */
  217. public function getRightColumns()
  218. {
  219. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignColumns() : $this->getLocalColumns();
  220. }
  221. /**
  222. * Set the onUpdate behavior
  223. *
  224. * @param string $onUpdate
  225. */
  226. public function setOnUpdate($onUpdate)
  227. {
  228. $this->onUpdate = $onUpdate;
  229. }
  230. /**
  231. * Get the onUpdate behavior
  232. *
  233. * @return integer the relation type
  234. */
  235. public function getOnUpdate()
  236. {
  237. return $this->onUpdate;
  238. }
  239. /**
  240. * Set the onDelete behavior
  241. *
  242. * @param string $onDelete
  243. */
  244. public function setOnDelete($onDelete)
  245. {
  246. $this->onDelete = $onDelete;
  247. }
  248. /**
  249. * Get the onDelete behavior
  250. *
  251. * @return integer the relation type
  252. */
  253. public function getOnDelete()
  254. {
  255. return $this->onDelete;
  256. }
  257. /**
  258. * Gets the symmetrical relation
  259. *
  260. * @return RelationMap
  261. */
  262. public function getSymmetricalRelation()
  263. {
  264. $localMapping = array($this->getLeftColumns(), $this->getRightColumns());
  265. foreach ($this->getRightTable()->getRelations() as $relation) {
  266. if ($localMapping == array($relation->getRightColumns(), $relation->getLeftColumns())) {
  267. return $relation;
  268. }
  269. }
  270. }
  271. }