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 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_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: 7490 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
34
{
35
    /**
36
     * getRelationDql
37
     *
38
     * @param integer $count
39
     * @return string
40
     */
41
    public function getRelationDql($count, $context = 'record')
42
    {
43
        switch ($context) {
44
            case 'record':
45
                $identifierColumnNames = $this->definition['table']->getIdentifierColumnNames();
46
                $identifier = array_pop($identifierColumnNames);
47
                $sub    = 'SELECT '.$this->definition['foreign']
48
                        . ' FROM '.$this->definition['refTable']->getTableName()
49
                        . ' WHERE '.$this->definition['local']
50
                        . ' = ?';
51
 
52
                $sub2   = 'SELECT '.$this->definition['local']
53
                        . ' FROM '.$this->definition['refTable']->getTableName()
54
                        . ' WHERE '.$this->definition['foreign']
55
                        . ' = ?';
56
 
57
                $dql  = 'FROM ' . $this->definition['table']->getComponentName()
58
                      . '.' . $this->definition['refTable']->getComponentName()
59
                      . ' WHERE ' . $this->definition['table']->getComponentName()
60
                      . '.' . $identifier
61
                      . ' IN (' . $sub . ')'
62
                      . ' || ' . $this->definition['table']->getComponentName()
63
                      . '.' . $identifier
64
                      . ' IN (' . $sub2 . ')';
65
 
66
                $dql .= $this->getOrderBy($this->definition['table']->getComponentName(), false);
67
                break;
68
            case 'collection':
69
                $sub  = substr(str_repeat('?, ', $count),0,-2);
70
                $dql  = 'FROM '.$this->definition['refTable']->getComponentName()
71
                      . '.' . $this->definition['table']->getComponentName()
72
                      . ' WHERE '.$this->definition['refTable']->getComponentName()
73
                      . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
74
 
75
                $dql .= $this->getOrderBy($this->definition['refTable']->getComponentName(), false);
76
        };
77
 
78
        return $dql;
79
    }
80
 
81
    public function fetchRelatedFor(Doctrine_Record $record)
82
    {
83
        $id      = $record->getIncremented();
84
 
85
        $q = new Doctrine_RawSql();
86
 
87
        $assocTable = $this->getAssociationFactory()->getTableName();
88
        $tableName  = $record->getTable()->getTableName();
89
        $identifierColumnNames = $record->getTable()->getIdentifierColumnNames();
90
        $identifier = array_pop($identifierColumnNames);
91
 
92
        $sub     = 'SELECT '.$this->getForeign().
93
                   ' FROM '.$assocTable.
94
                   ' WHERE '.$this->getLocal().
95
                   ' = ?';
96
 
97
        $sub2   = 'SELECT '.$this->getLocal().
98
                  ' FROM '.$assocTable.
99
                  ' WHERE '.$this->getForeign().
100
                  ' = ?';
101
 
102
        $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
103
          ->from($tableName . ' INNER JOIN '.$assocTable.' ON '.
104
                 $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal() . ' OR ' .
105
                 $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign()
106
                 )
107
          ->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
108
                  $tableName.'.'.$identifier.' IN ('.$sub2.')'
109
                );
110
        $q->addComponent($tableName,  $record->getTable()->getComponentName());
111
        $q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
112
        $q->orderBy($this->getOrderByStatement($tableName, true));
113
 
114
        return $q->execute(array($id, $id));
115
    }
116
}