Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: ForeignKey.php 7490 2010-03-29 19:53:27Z jwage $
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
/**
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.doctrine-project.org
31
 * @since       1.0
32
 * @version     $Revision: 7490 $
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
        $localTable = $record->getTable();
48
        foreach ((array) $this->definition['local'] as $local) {
49
           $value = $record->get($localTable->getFieldName($local));
50
           if (isset($value)) {
51
               $id[] = $value;
52
           }
53
        }
54
        if ($this->isOneToOne()) {
55
            if ( ! $record->exists() || empty($id) ||
56
                 ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) {
57
 
58
                $related = $this->getTable()->create();
59
            } else {
60
                $dql  = 'FROM ' . $this->getTable()->getComponentName()
61
                      . ' WHERE ' . $this->getCondition() . $this->getOrderBy(null, false);
62
 
63
                $coll = $this->getTable()->getConnection()->query($dql, $id);
64
                $related = $coll[0];
65
            }
66
 
67
            $related->set($related->getTable()->getFieldName($this->definition['foreign']),
68
                    $record, false);
69
        } else {
70
 
71
            if ( ! $record->exists() || empty($id) ||
72
                 ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) {
73
 
74
                $related = Doctrine_Collection::create($this->getTable());
75
            } else {
76
                $query      = $this->getRelationDql(1);
77
                $related    = $this->getTable()->getConnection()->query($query, $id);
78
            }
79
            $related->setReference($record, $this);
80
        }
81
        return $related;
82
    }
83
 
84
    /**
85
     * getCondition
86
     *
87
     * @param string $alias
88
     */
89
    public function getCondition($alias = null)
90
    {
91
        if ( ! $alias) {
92
           $alias = $this->getTable()->getComponentName();
93
        }
94
        $conditions = array();
95
        foreach ((array) $this->definition['foreign'] as $foreign) {
96
            $conditions[] = $alias . '.' . $foreign . ' = ?';
97
        }
98
        return implode(' AND ', $conditions);
99
    }
100
}