# Source for file Self.php

Documentation is available at Self.php

1. <?php
2. /*
3. * $Id: Self.php 1434 2007-05-22 15:57:17Z zYne $
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.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Relation_Association');
22. /**
23. * Doctrine_Relation_Association_Self
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision: 1434 $
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
34. {
35. /**
36. * getRelationDql
37. *
38. * @param integer $count
39. * @return string
40. */
41. public function getRelationDql($count, $context = 'record')
42. {
43. switch ($context) {
44. case 'record':
45. $sub = 'SELECT '.$this->definition['foreign']
46. . ' FROM '.$this->definition['refTable']->getTableName()
47. . ' WHERE '.$this->definition['local']
48. . ' = ?';
50. $sub2 = 'SELECT '.$this->definition['local']
51. . ' FROM '.$this->definition['refTable']->getTableName()
52. . ' WHERE '.$this->definition['foreign']
53. . ' = ?';
55. $dql = 'FROM ' . $this->definition['table']->getComponentName()
56. . '.' . $this->definition['refTable']->getComponentName()
57. . ' WHERE ' . $this->definition['table']->getComponentName()
58. . '.' . $this->definition['table']->getIdentifier()
59. . ' IN (' . $sub . ')'
60. . ' || ' . $this->definition['table']->getComponentName()
61. . '.' . $this->definition['table']->getIdentifier()
62. . ' IN (' . $sub2 . ')';
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. };
72. return $dql;
73. }
75. public function fetchRelatedFor(Doctrine_Record $record)
76. {
77. $id = $record->getIncremented();
79. $q = new Doctrine_RawSql();
81. $assocTable = $this->getAssociationFactory()->getTableName();
82. $tableName = $record->getTable()->getTableName();
83. $identifier = $record->getTable()->getIdentifier();
85. $sub = 'SELECT '.$this->getForeign().
86. ' FROM '.$assocTable.
87. ' WHERE '.$this->getLocal().
88. ' = ?';
90. $sub2 = 'SELECT '.$this->getLocal().
91. ' FROM '.$assocTable.
92. ' WHERE '.$this->getForeign().
93. ' = ?';
95. $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
96. ->from($tableName . ' INNER JOIN '.$assocTable.' ON '.
97. $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal() . ' OR ' .
98. $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign()
99. )
100. ->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
101. $tableName.'.'.$identifier.' IN ('.$sub2.')'
102. );
103. $q->addComponent($tableName, $record->getTable()->getComponentName());
104. $q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
106. return $q->execute(array($id, $id));
107. }
108. }