# Coverage for Doctrine_Relation_ForeignKey

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: ForeignKey.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
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.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Relation');
22 | /**
23 | * Doctrine_Relation_ForeignKey
24 | * This class represents a foreign key relation
25 | *
26 | * @package Doctrine
27 | * @subpackage Relation
28 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 | * @link www.phpdoctrine.org
31 | * @since 1.0
32 | * @version $Revision: 2963 $
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)) {
56 |
57 | $related = $this->getTable()->create();
58 | } else {
59 | $dql = 'FROM ' . $this->getTable()->getComponentName()
60 | . ' WHERE ' . $this->getCondition();
61 |
62 | $coll = $this->getTable()->getConnection()->query($dql, $id);
63 | $related = $coll[0];
64 | }
65 |
66 | $related->set($this->definition['foreign'], $record, false);
67 |
68 | } else {
69 |
70 | if ( ! $record->exists() || empty($id) ||
71 | ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
72 |
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 | /**
84 | * getCondition
85 | *
86 | * @param string $alias
87 | */
88 | public function getCondition($alias = null)
89 | {
90 | if ( ! $alias) {
91 | $alias = $this->getTable()->getComponentName();
92 | }
93 | $conditions = array();
94 | foreach ((array) $this->definition['foreign'] as $foreign) {
95 | $conditions[] = $alias . '.' . $foreign . ' = ?';
96 | }
97 | return implode(' AND ', $conditions);
98 | }
99 | }