# Source for file ForeignKey.php

Documentation is available at ForeignKey.php

1. <?php
2. /*
3. * $Id: ForeignKey.php 1517 2007-05-30 10:20:21Z 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');
22. /**
23. * Doctrine_Relation_ForeignKey
24. * This class represents a foreign key relation
25. *
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @package Doctrine
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. * @version $Revision: 1517 $
33. */
34. class Doctrine_Relation_ForeignKey extends Doctrine_Relation
35. {
36. /**
37. * fetchRelatedFor
38. *
39. * fetches a component related to given record
40. *
41. * @param Doctrine_Record $record
42. * @return Doctrine_Record|Doctrine_Collection
43. */
44. public function fetchRelatedFor(Doctrine_Record $record)
45. {
46. $id = array();
47. foreach ((array) $this->definition['local'] as $local) {
48. $value = $record->get($local);
49. if (isset($value)) {
50. $id[] = $value;
51. }
52. }
53. if ($this->isOneToOne()) {
54. if ( ! $record->exists() || empty($id) ||
55. ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
57. $related = $this->getTable()->create();
58. } else {
59. $dql = 'FROM ' . $this->getTable()->getComponentName()
60. . ' WHERE ' . $this->getCondition();
62. $coll = $this->getTable()->getConnection()->query($dql, $id);
63. $related = $coll[0];
64. }
66. $related->set($this->definition['foreign'], $record, false);
68. } else {
70. if ( ! $record->exists() || empty($id) ||
71. ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
73. $related = new Doctrine_Collection($this->getTable());
74. } else {
75. $query = $this->getRelationDql(1);
76. $related = $this->getTable()->getConnection()->query($query, $id);
77. }
78. $related->setReference($record, $this);
79. }
80. return $related;
81. }
82. /**
83. * getCondition
84. *
85. * @param string $alias
86. */
87. public function getCondition($alias = null)
88. {
89. if ( ! $alias) {
90. $alias = $this->getTable()->getComponentName();
91. }
92. $conditions = array();
93. foreach ((array) $this->definition['foreign'] as $foreign) {
94. $conditions[] = $alias . '.' . $foreign . ' = ?';
95. }
96. return implode(' AND ', $conditions);
97. }
98. }