ModelJoin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * A ModelJoin is a Join object tied to a RelationMap object
  11. *
  12. * @author Francois Zaninotto (Propel)
  13. * @package propel.runtime.query
  14. */
  15. class ModelJoin extends Join
  16. {
  17. protected $relationMap;
  18. protected $tableMap;
  19. protected $leftTableAlias;
  20. protected $relationAlias;
  21. protected $previousJoin;
  22. public function setRelationMap(RelationMap $relationMap, $leftTableAlias = null, $relationAlias = null)
  23. {
  24. $leftCols = $relationMap->getLeftColumns();
  25. $rightCols = $relationMap->getRightColumns();
  26. $nbColumns = $relationMap->countColumnMappings();
  27. for ($i=0; $i < $nbColumns; $i++) {
  28. $leftColName = ($leftTableAlias ? $leftTableAlias : $leftCols[$i]->getTableName()) . '.' . $leftCols[$i]->getName();
  29. $rightColName = ($relationAlias ? $relationAlias : $rightCols[$i]->getTableName()) . '.' . $rightCols[$i]->getName();
  30. $this->addCondition($leftColName, $rightColName, Criteria::EQUAL);
  31. }
  32. $this->relationMap = $relationMap;
  33. $this->leftTableAlias = $leftTableAlias;
  34. $this->relationAlias = $relationAlias;
  35. return $this;
  36. }
  37. public function getRelationMap()
  38. {
  39. return $this->relationMap;
  40. }
  41. /**
  42. * Sets the right tableMap for this join
  43. *
  44. * @param TableMap $tableMap The table map to use
  45. *
  46. * @return ModelJoin The current join object, for fluid interface
  47. */
  48. public function setTableMap(TableMap $tableMap)
  49. {
  50. $this->tableMap = $tableMap;
  51. return $this;
  52. }
  53. /**
  54. * Gets the right tableMap for this join
  55. *
  56. * @return TableMap The table map
  57. */
  58. public function getTableMap()
  59. {
  60. if (null === $this->tableMap && null !== $this->relationMap)
  61. {
  62. $this->tableMap = $this->relationMap->getRightTable();
  63. }
  64. return $this->tableMap;
  65. }
  66. public function setPreviousJoin(ModelJoin $join)
  67. {
  68. $this->previousJoin = $join;
  69. return $this;
  70. }
  71. public function getPreviousJoin()
  72. {
  73. return $this->previousJoin;
  74. }
  75. public function isPrimary()
  76. {
  77. return null === $this->previousJoin;
  78. }
  79. public function setLeftTableAlias($leftTableAlias)
  80. {
  81. $this->leftTableAlias = $leftTableAlias;
  82. return $this;
  83. }
  84. public function getLeftTableAlias()
  85. {
  86. return $this->leftTableAlias;
  87. }
  88. public function hasLeftTableAlias()
  89. {
  90. return null !== $this->leftTableAlias;
  91. }
  92. public function setRelationAlias($relationAlias)
  93. {
  94. $this->relationAlias = $relationAlias;
  95. return $this;
  96. }
  97. public function getRelationAlias()
  98. {
  99. return $this->relationAlias;
  100. }
  101. public function hasRelationAlias()
  102. {
  103. return null !== $this->relationAlias;
  104. }
  105. /**
  106. * This method returns the last related, but already hydrated object up until this join
  107. * Starting from $startObject and continuously calling the getters to get
  108. * to the base object for the current join.
  109. *
  110. * This method only works if PreviousJoin has been defined,
  111. * which only happens when you provide dotted relations when calling join
  112. *
  113. * @param Object $startObject the start object all joins originate from and which has already hydrated
  114. * @return Object the base Object of this join
  115. */
  116. public function getObjectToRelate($startObject)
  117. {
  118. if($this->isPrimary()) {
  119. return $startObject;
  120. } else {
  121. $previousJoin = $this->getPreviousJoin();
  122. $previousObject = $previousJoin->getObjectToRelate($startObject);
  123. $method = 'get' . $previousJoin->getRelationMap()->getName();
  124. return $previousObject->$method();
  125. }
  126. }
  127. public function equals($join)
  128. {
  129. return parent::equals($join)
  130. && $this->relationMap == $join->getRelationMap()
  131. && $this->previousJoin == $join->getPreviousJoin()
  132. && $this->relationAlias == $join->getRelationAlias();
  133. }
  134. public function __toString()
  135. {
  136. return parent::toString()
  137. . ' tableMap: ' . ($this->tableMap ? get_class($this->tableMap) : 'null')
  138. . ' relationMap: ' . $this->relationMap->getName()
  139. . ' previousJoin: ' . ($this->previousJoin ? '(' . $this->previousJoin . ')' : 'null')
  140. . ' relationAlias: ' . $this->relationAlias;
  141. }
  142. }