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: LocalKey.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_LocalKey
24
 * This class represents a local 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_LocalKey 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
        $localFieldName = $record->getTable()->getFieldName($this->definition['local']);
47
        $id = $record->get($localFieldName);
48
 
49
        if (is_null($id) || ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) {
50
            $related = $this->getTable()->create();
51
 
52
            // Ticket #1131 Patch.
53
            if ( ! is_null($id)) {
54
                $related->assignIdentifier($id);
55
                $related->state(Doctrine_Record::STATE_PROXY);
56
            }
57
        } else {
58
            $dql  = 'FROM ' . $this->getTable()->getComponentName()
59
                 . ' WHERE ' . $this->getCondition() . $this->getOrderBy(null, false);
60
 
61
            $related = $this->getTable()
62
                            ->getConnection()
63
                            ->query($dql, array($id))
64
                            ->getFirst();
65
 
66
            if ( ! $related || empty($related)) {
67
                $related = $this->getTable()->create();
68
            }
69
        }
70
 
71
        $record->set($localFieldName, $id, false);
72
 
73
        return $related;
74
    }
75
 
76
    /**
77
     * getCondition
78
     *
79
     * @param string $alias
80
     */
81
    public function getCondition($alias = null)
82
    {
83
        if ( ! $alias) {
84
           $alias = $this->getTable()->getComponentName();
85
        }
86
        return $alias . '.' . $this->definition['foreign'] . ' = ?';
87
    }
88
}