PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/propel/runtime/lib/map/RelationMap.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 321 lines | 138 code | 31 blank | 152 comment | 14 complexity | 362c8756482485a46e79c8f5b4979e88 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  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$
  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. $pluralName,
  37. $type,
  38. $localTable,
  39. $foreignTable,
  40. $localColumns = array(),
  41. $foreignColumns = array(),
  42. $onUpdate, $onDelete;
  43. /**
  44. * Constructor.
  45. *
  46. * @param string $name Name of the relation.
  47. * @param string $pluralName Plural Name of the relation.
  48. * Defaults to the Name of the relation concatenated with 's'.
  49. */
  50. public function __construct($name)
  51. {
  52. $this->name = $name;
  53. }
  54. /**
  55. * Get the name of this relation.
  56. *
  57. * @return string The name of the relation.
  58. */
  59. public function getName()
  60. {
  61. return $this->name;
  62. }
  63. public function setPluralName($pluralName)
  64. {
  65. $this->pluralName = $pluralName;
  66. }
  67. /**
  68. * Get the plural name of this relation.
  69. *
  70. * @return string The plural name of the relation.
  71. */
  72. public function getPluralName()
  73. {
  74. return null !== $this->pluralName ? $this->pluralName : ($this->name . 's');
  75. }
  76. /**
  77. * Set the type
  78. *
  79. * @param integer $type The relation type (either self::MANY_TO_ONE, self::ONE_TO_MANY, or self::ONE_TO_ONE)
  80. */
  81. public function setType($type)
  82. {
  83. $this->type = $type;
  84. }
  85. /**
  86. * Get the type
  87. *
  88. * @return integer the relation type
  89. */
  90. public function getType()
  91. {
  92. return $this->type;
  93. }
  94. /**
  95. * Set the local table
  96. *
  97. * @param TableMap $table The local table for this relationship
  98. */
  99. public function setLocalTable($table)
  100. {
  101. $this->localTable = $table;
  102. }
  103. /**
  104. * Get the local table
  105. *
  106. * @return TableMap The local table for this relationship
  107. */
  108. public function getLocalTable()
  109. {
  110. return $this->localTable;
  111. }
  112. /**
  113. * Set the foreign table
  114. *
  115. * @param TableMap $table The foreign table for this relationship
  116. */
  117. public function setForeignTable($table)
  118. {
  119. $this->foreignTable = $table;
  120. }
  121. /**
  122. * Get the foreign table
  123. *
  124. * @return TableMap The foreign table for this relationship
  125. */
  126. public function getForeignTable()
  127. {
  128. return $this->foreignTable;
  129. }
  130. /**
  131. * Get the left table of the relation
  132. *
  133. * @return TableMap The left table for this relationship
  134. */
  135. public function getLeftTable()
  136. {
  137. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalTable() : $this->getForeignTable();
  138. }
  139. /**
  140. * Get the right table of the relation
  141. *
  142. * @return TableMap The right table for this relationship
  143. */
  144. public function getRightTable()
  145. {
  146. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignTable() : $this->getLocalTable();
  147. }
  148. /**
  149. * Add a column mapping
  150. *
  151. * @param ColumnMap $local The local column
  152. * @param ColumnMap $foreign The foreign column
  153. */
  154. public function addColumnMapping(ColumnMap $local, ColumnMap $foreign)
  155. {
  156. $this->localColumns[] = $local;
  157. $this->foreignColumns[] = $foreign;
  158. }
  159. /**
  160. * Get an associative array mapping local column names to foreign column names
  161. * The arrangement of the returned array depends on the $direction parameter:
  162. * - If the value is RelationMap::LOCAL_TO_FOREIGN, then the returned array is local => foreign
  163. * - If the value is RelationMap::LEFT_TO_RIGHT, then the returned array is left => right
  164. *
  165. * @param int $direction How the associative array must return columns
  166. * @return Array Associative array (local => foreign) of fully qualified column names
  167. */
  168. public function getColumnMappings($direction = RelationMap::LOCAL_TO_FOREIGN)
  169. {
  170. $h = array();
  171. if ($direction == RelationMap::LEFT_TO_RIGHT && $this->getType() == RelationMap::MANY_TO_ONE) {
  172. $direction = RelationMap::LOCAL_TO_FOREIGN;
  173. }
  174. for ($i=0, $size=count($this->localColumns); $i < $size; $i++) {
  175. if ($direction == RelationMap::LOCAL_TO_FOREIGN) {
  176. $h[$this->localColumns[$i]->getFullyQualifiedName()] = $this->foreignColumns[$i]->getFullyQualifiedName();
  177. } else {
  178. $h[$this->foreignColumns[$i]->getFullyQualifiedName()] = $this->localColumns[$i]->getFullyQualifiedName();
  179. }
  180. }
  181. return $h;
  182. }
  183. /**
  184. * Returns true if the relation has more than one column mapping
  185. *
  186. * @return boolean
  187. */
  188. public function isComposite()
  189. {
  190. return $this->countColumnMappings() > 1;
  191. }
  192. /**
  193. * Return the number of column mappings
  194. *
  195. * @return int
  196. */
  197. public function countColumnMappings()
  198. {
  199. return count($this->localColumns);
  200. }
  201. /**
  202. * Get the local columns
  203. *
  204. * @return Array list of ColumnMap objects
  205. */
  206. public function getLocalColumns()
  207. {
  208. return $this->localColumns;
  209. }
  210. /**
  211. * Get the foreign columns
  212. *
  213. * @return Array list of ColumnMap objects
  214. */
  215. public function getForeignColumns()
  216. {
  217. return $this->foreignColumns;
  218. }
  219. /**
  220. * Get the left columns of the relation
  221. *
  222. * @return array of ColumnMap objects
  223. */
  224. public function getLeftColumns()
  225. {
  226. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalColumns() : $this->getForeignColumns();
  227. }
  228. /**
  229. * Get the right columns of the relation
  230. *
  231. * @return array of ColumnMap objects
  232. */
  233. public function getRightColumns()
  234. {
  235. return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignColumns() : $this->getLocalColumns();
  236. }
  237. /**
  238. * Set the onUpdate behavior
  239. *
  240. * @param string $onUpdate
  241. */
  242. public function setOnUpdate($onUpdate)
  243. {
  244. $this->onUpdate = $onUpdate;
  245. }
  246. /**
  247. * Get the onUpdate behavior
  248. *
  249. * @return integer the relation type
  250. */
  251. public function getOnUpdate()
  252. {
  253. return $this->onUpdate;
  254. }
  255. /**
  256. * Set the onDelete behavior
  257. *
  258. * @param string $onDelete
  259. */
  260. public function setOnDelete($onDelete)
  261. {
  262. $this->onDelete = $onDelete;
  263. }
  264. /**
  265. * Get the onDelete behavior
  266. *
  267. * @return integer the relation type
  268. */
  269. public function getOnDelete()
  270. {
  271. return $this->onDelete;
  272. }
  273. /**
  274. * Gets the symmetrical relation
  275. *
  276. * @return RelationMap
  277. *
  278. * @throws PropelException
  279. */
  280. public function getSymmetricalRelation()
  281. {
  282. $localMapping = array($this->getLeftColumns(), $this->getRightColumns());
  283. foreach ($this->getRightTable()->getRelations() as $relation) {
  284. if ($localMapping == array($relation->getRightColumns(), $relation->getLeftColumns())) {
  285. return $relation;
  286. }
  287. }
  288. throw new PropelException('The relation could not be resolved.');
  289. }
  290. }