~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to library/components/doctrine/lib/Doctrine/Relation/Nest.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.phpdoctrine.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.phpdoctrine.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
    /**
 
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
                break;
 
66
            case 'collection':
 
67
                $sub  = substr(str_repeat('?, ', $count),0,-2);
 
68
                $dql  = 'FROM '.$this->definition['refTable']->getComponentName()
 
69
                      . '.' . $this->definition['table']->getComponentName()
 
70
                      . ' WHERE '.$this->definition['refTable']->getComponentName()
 
71
                      . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
 
72
        };
 
73
 
 
74
        return $dql;
 
75
    }*/
 
76
 
 
77
    public function fetchRelatedFor(Doctrine_Record $record)
 
78
    {
 
79
        $id = $record->getIncremented();
 
80
 
 
81
        if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
 
82
            return new Doctrine_Collection($this->getTable());
 
83
        } else {
 
84
            $q = new Doctrine_RawSql($this->getTable()->getConnection());
 
85
 
 
86
            $assocTable = $this->getAssociationFactory()->getTableName();
 
87
            $tableName  = $record->getTable()->getTableName();
 
88
            $identifierColumnNames = $record->getTable()->getIdentifierColumnNames();
 
89
            $identifier = array_pop($identifierColumnNames);
 
90
    
 
91
            $sub = 'SELECT ' . $this->getForeignRefColumnName()
 
92
                 . ' FROM ' . $assocTable 
 
93
                 . ' WHERE ' . $this->getLocalRefColumnName() 
 
94
                 . ' = ?';
 
95
 
 
96
            $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub . ')';
 
97
            $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeignRefColumnName();
 
98
 
 
99
            if ($this->definition['equal']) {
 
100
                $sub2   = 'SELECT ' . $this->getLocalRefColumnName()
 
101
                        . ' FROM '  . $assocTable
 
102
                        . ' WHERE ' . $this->getForeignRefColumnName()
 
103
                        . ' = ?';
 
104
 
 
105
                $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub2 . ')';
 
106
                $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocalRefColumnName();
 
107
            }
 
108
            $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
 
109
              ->from($tableName . ' INNER JOIN ' . $assocTable . ' ON ' . implode(' OR ', $joinCondition))
 
110
              ->where(implode(' OR ', $condition));
 
111
            $q->addComponent($tableName,  $this->getClass());
 
112
            
 
113
            $path = $this->getClass(). '.' . $this->getAssociationFactory()->getComponentName();
 
114
            if ($this->definition['refClassRelationAlias']) {
 
115
                $path = $this->getClass(). '.' . $this->definition['refClassRelationAlias'];
 
116
            }
 
117
            $q->addComponent($assocTable, $path);
 
118
 
 
119
            $params = ($this->definition['equal']) ? array($id, $id) : array($id);
 
120
            $res = $q->execute($params);
 
121
 
 
122
            return $res;
 
123
        }
 
124
    }
 
125
}
 
 
b'\\ No newline at end of file'