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: Self.php 1434 2007-05-22 15:57:17Z 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.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Relation_Association_Self
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Relation
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 1434 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Relation_Nest extends Doctrine_Relation_Association
34
{
35
    public function fetchRelatedFor(Doctrine_Record $record)
36
    {
37
        $id = $record->getIncremented();
38
 
39
        if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) {
40
            return Doctrine_Collection::create($this->getTable());
41
        } else {
42
            $q = new Doctrine_RawSql($this->getTable()->getConnection());
43
            $formatter = $q->getConnection()->formatter;
44
 
45
            $assocTable = $this->getAssociationFactory()->getTableName();
46
            $tableName  = $record->getTable()->getTableName();
47
            $identifierColumnNames = $record->getTable()->getIdentifierColumnNames();
48
            $identifier = $formatter->quoteIdentifier(array_pop($identifierColumnNames));
49
 
50
            $sub = 'SELECT ' . $formatter->quoteIdentifier($this->getForeignRefColumnName())
51
                 . ' FROM '  . $formatter->quoteIdentifier($assocTable)
52
                 . ' WHERE ' . $formatter->quoteIdentifier($this->getLocalRefColumnName())
53
                 . ' = ?';
54
 
55
            $condition[] = $formatter->quoteIdentifier($tableName) . '.' . $identifier . ' IN (' . $sub . ')';
56
            $joinCondition[] = $formatter->quoteIdentifier($tableName) . '.' . $identifier . ' = ' . $formatter->quoteIdentifier($assocTable) . '.' . $formatter->quoteIdentifier($this->getForeignRefColumnName());
57
 
58
            if ($this->definition['equal']) {
59
                $sub2   = 'SELECT ' . $formatter->quoteIdentifier($this->getLocalRefColumnName())
60
                        . ' FROM '  . $formatter->quoteIdentifier($assocTable)
61
                        . ' WHERE ' . $formatter->quoteIdentifier($this->getForeignRefColumnName())
62
                        . ' = ?';
63
 
64
                $condition[] = $formatter->quoteIdentifier($tableName) . '.' . $identifier . ' IN (' . $sub2 . ')';
65
                $joinCondition[] = $formatter->quoteIdentifier($tableName) . '.' . $identifier . ' = ' . $formatter->quoteIdentifier($assocTable) . '.' . $formatter->quoteIdentifier($this->getLocalRefColumnName());
66
            }
67
            $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
68
              ->from($formatter->quoteIdentifier($tableName) . ' INNER JOIN ' . $formatter->quoteIdentifier($assocTable) . ' ON ' . implode(' OR ', $joinCondition))
69
              ->where(implode(' OR ', $condition));
70
            if ($orderBy = $this->getOrderByStatement($tableName, true)) {
71
                $q->addOrderBy($orderBy);
72
            } else {
73
                $q->addOrderBy($formatter->quoteIdentifier($tableName) . '.' . $identifier . ' ASC');
74
            }
75
            $q->addComponent($tableName,  $this->getClass());
76
 
77
            $path = $this->getClass(). '.' . $this->getAssociationFactory()->getComponentName();
78
            if ($this->definition['refClassRelationAlias']) {
79
                $path = $this->getClass(). '.' . $this->definition['refClassRelationAlias'];
80
            }
81
            $q->addComponent($assocTable, $path);
82
 
83
            $params = ($this->definition['equal']) ? array($id, $id) : array($id);
84
 
85
            $res = $q->execute($params);
86
 
87
            return $res;
88
        }
89
    }
90
}