/libraries/rokcommon/Doctrine/Relation/Association/Self.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 116 lines · 67 code · 13 blank · 36 comment · 1 complexity · 286ce2ee5db98c722bcc6a171ba21570 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: Self.php 48519 2012-02-03 23:18:52Z btowles $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Relation_Association_Self
  23. *
  24. * @package Doctrine
  25. * @subpackage Relation
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 1.0
  29. * @version $Revision: 7490 $
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
  33. {
  34. /**
  35. * getRelationDql
  36. *
  37. * @param integer $count
  38. * @return string
  39. */
  40. public function getRelationDql($count, $context = 'record')
  41. {
  42. switch ($context) {
  43. case 'record':
  44. $identifierColumnNames = $this->definition['table']->getIdentifierColumnNames();
  45. $identifier = array_pop($identifierColumnNames);
  46. $sub = 'SELECT '.$this->definition['foreign']
  47. . ' FROM '.$this->definition['refTable']->getTableName()
  48. . ' WHERE '.$this->definition['local']
  49. . ' = ?';
  50. $sub2 = 'SELECT '.$this->definition['local']
  51. . ' FROM '.$this->definition['refTable']->getTableName()
  52. . ' WHERE '.$this->definition['foreign']
  53. . ' = ?';
  54. $dql = 'FROM ' . $this->definition['table']->getComponentName()
  55. . '.' . $this->definition['refTable']->getComponentName()
  56. . ' WHERE ' . $this->definition['table']->getComponentName()
  57. . '.' . $identifier
  58. . ' IN (' . $sub . ')'
  59. . ' || ' . $this->definition['table']->getComponentName()
  60. . '.' . $identifier
  61. . ' IN (' . $sub2 . ')';
  62. $dql .= $this->getOrderBy($this->definition['table']->getComponentName(), false);
  63. break;
  64. case 'collection':
  65. $sub = substr(str_repeat('?, ', $count),0,-2);
  66. $dql = 'FROM '.$this->definition['refTable']->getComponentName()
  67. . '.' . $this->definition['table']->getComponentName()
  68. . ' WHERE '.$this->definition['refTable']->getComponentName()
  69. . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
  70. $dql .= $this->getOrderBy($this->definition['refTable']->getComponentName(), false);
  71. };
  72. return $dql;
  73. }
  74. public function fetchRelatedFor(Doctrine_Record $record)
  75. {
  76. $id = $record->getIncremented();
  77. $q = new Doctrine_RawSql();
  78. $assocTable = $this->getAssociationFactory()->getTableName();
  79. $tableName = $record->getTable()->getTableName();
  80. $identifierColumnNames = $record->getTable()->getIdentifierColumnNames();
  81. $identifier = array_pop($identifierColumnNames);
  82. $sub = 'SELECT '.$this->getForeign().
  83. ' FROM '.$assocTable.
  84. ' WHERE '.$this->getLocal().
  85. ' = ?';
  86. $sub2 = 'SELECT '.$this->getLocal().
  87. ' FROM '.$assocTable.
  88. ' WHERE '.$this->getForeign().
  89. ' = ?';
  90. $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
  91. ->from($tableName . ' INNER JOIN '.$assocTable.' ON '.
  92. $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal() . ' OR ' .
  93. $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign()
  94. )
  95. ->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
  96. $tableName.'.'.$identifier.' IN ('.$sub2.')'
  97. );
  98. $q->addComponent($tableName, $record->getTable()->getComponentName());
  99. $q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
  100. $q->orderBy($this->getOrderByStatement($tableName, true));
  101. return $q->execute(array($id, $id));
  102. }
  103. }