Coverage for Doctrine_Relation_Nest

Back to coverage report

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 Doctrine::autoload('Doctrine_Relation_Association');
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     /**
78     public function fetchRelatedFor(Doctrine_Record $record)
79     {
80         $id = $record->getIncremented();
81
82         if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
83             return new Doctrine_Collection($this->getTable());
84         } else {
85             $q = new Doctrine_Query();
86             
87             $c  = $this->getTable()->getComponentName();
88             $a  = substr($c, 0, 1);
89             $c2 = $this->getAssociationTable()->getComponentName();
90             $a2 = substr($c2, 0, 1);
91
92             $q->from($c)
93               ->innerJoin($c . '.' . $c2)
94
95             $sub = 'SELECT ' . $this->getForeign() 
96                  . ' FROM '  . $c2
97                  . ' WHERE ' . $this->getLocal() 
98                  . ' = ?';
99         }
100     }
101     */
102
103     public function fetchRelatedFor(Doctrine_Record $record)
104     {
105         $id = $record->getIncremented();
106
107
108         if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
109             return new Doctrine_Collection($this->getTable());
110         } else {
111             $q = new Doctrine_RawSql($this->getTable()->getConnection());
112
113             $assocTable = $this->getAssociationFactory()->getTableName();
114             $tableName  = $record->getTable()->getTableName();
115             $identifierColumnNames = $record->getTable()->getIdentifierColumnNames();
116             $identifier = array_pop($identifierColumnNames);
117     
118             $sub = 'SELECT ' . $this->getForeign()
119                  . ' FROM ' . $assocTable 
120                  . ' WHERE ' . $this->getLocal() 
121                  . ' = ?';
122
123             $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub . ')';
124             $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign();
125
126             if ($this->definition['equal']) {
127                 $sub2   = 'SELECT ' . $this->getLocal()
128                         . ' FROM '  . $assocTable
129                         . ' WHERE ' . $this->getForeign()
130                         . ' = ?';
131
132                 $condition[] = $tableName . '.' . $identifier . ' IN (' . $sub2 . ')';
133                 $joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal();
134             }
135             $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
136               ->from($tableName . ' INNER JOIN ' . $assocTable . ' ON ' . implode(' OR ', $joinCondition))
137               ->where(implode(' OR ', $condition));
138             $q->addComponent($tableName,  $record->getTable()->getComponentName());
139             $q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
140
141             $params = ($this->definition['equal']) ? array($id, $id) : array($id);
142
143             return $q->execute($params);
144         }
145     }
146 }