# Source for file Association.php

Documentation is available at Association.php

1. <?php
2. /*
3. * $Id: Association.php 2212 2007-08-11 18:24:19Z nightfreak $
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');
22. /**
23. * Doctrine_Relation_Association this class takes care of association mapping
24. * (= many-to-many relationships, where the relationship is handled with an additional relational table
25. * which holds 2 foreign keys)
26. *
27. *
28. * @package Doctrine
29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
32. * @since 1.0
33. * @version $Revision: 2212 $
34. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
35. */
36. class Doctrine_Relation_Association extends Doctrine_Relation
37. {
38. /**
39. * @return Doctrine_Table
40. */
41. public function getAssociationFactory()
42. {
43. return $this->definition['refTable'];
44. }
45. public function getAssociationTable()
46. {
47. return $this->definition['refTable'];
48. }
49. /**
50. * getRelationDql
51. *
52. * @param integer $count
53. * @return string
54. */
55. public function getRelationDql($count, $context = 'record')
56. {
57. $component = $this->definition['refTable']->getComponentName();
58. switch ($context) {
59. case "record":
60. $sub = substr(str_repeat("?, ", $count),0,-2);
61. $dql = 'FROM ' . $this->getTable()->getComponentName();
62. $dql .= '.' . $component;
63. $dql .= ' WHERE ' . $this->getTable()->getComponentName()
64. . '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
65. break;
66. case "collection":
67. $sub = substr(str_repeat("?, ", $count),0,-2);
68. $dql = 'FROM ' . $component . '.' . $this->getTable()->getComponentName();
69. $dql .= ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
70. break;
71. }
73. return $dql;
74. }
75. /**
76. * fetchRelatedFor
77. *
78. * fetches a component related to given record
79. *
80. * @param Doctrine_Record $record
81. * @return Doctrine_Record|Doctrine_Collection
82. */
83. public function fetchRelatedFor(Doctrine_Record $record)
84. {
85. $id = $record->getIncremented();
86. if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
87. $coll = new Doctrine_Collection($this->getTable());
88. } else {
89. $coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id));
90. }
91. $coll->setReference($record, $this);
92. return $coll;
93. }
94. }