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.com>.
|
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.com
|
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 |
$sub = 'SELECT '.$this->definition['foreign']
|
46 |
. ' FROM '.$this->definition['refTable']->getTableName()
|
47 |
. ' WHERE '.$this->definition['local']
|
48 |
. ' = ?';
|
49 |
|
50 |
$sub2 = 'SELECT '.$this->definition['local']
|
51 |
. ' FROM '.$this->definition['refTable']->getTableName()
|
52 |
. ' WHERE '.$this->definition['foreign']
|
53 |
. ' = ?';
|
54 |
|
55 |
$dql = 'FROM ' . $this->definition['table']->getComponentName()
|
56 |
. '.' . $this->definition['refTable']->getComponentName()
|
57 |
. ' WHERE ' . $this->definition['table']->getComponentName()
|
58 |
. '.' . $this->definition['table']->getIdentifier()
|
59 |
. ' IN (' . $sub . ')'
|
60 |
. ' || ' . $this->definition['table']->getComponentName()
|
61 |
. '.' . $this->definition['table']->getIdentifier()
|
62 |
. ' IN (' . $sub2 . ')';
|
63 |
break;
|
64 |
case 'collection':
|
65 |
$sub = substr(str_repeat('?, ', $count),0,-2);
|
66 |
$dql = 'FROM '.$this->definition['refTable']->getComponentName()
|
67 |
. '.' . $this->definition['table']->getComponentName()
|
68 |
. ' WHERE '.$this->definition['refTable']->getComponentName()
|
69 |
. '.' . $this->definition['local'] . ' IN (' . $sub . ')';
|
70 |
};
|
71 |
|
72 |
return $dql;
|
73 |
}
|
74 |
|
75 |
/**
|
76 |
public function fetchRelatedFor(Doctrine_Record $record)
|
77 |
{
|
78 |
$id = $record->getIncremented();
|
79 |
|
80 |
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
|
81 |
return new Doctrine_Collection($this->getTable());
|
82 |
} else {
|
83 |
$q = new Doctrine_Query();
|
84 |
|
85 |
$c = $this->getTable()->getComponentName();
|
86 |
$a = substr($c, 0, 1);
|
87 |
$c2 = $this->getAssociationTable()->getComponentName();
|
88 |
$a2 = substr($c2, 0, 1);
|
89 |
|
90 |
$q->from($c)
|
91 |
->innerJoin($c . '.' . $c2)
|
92 |
|
93 |
$sub = 'SELECT ' . $this->getForeign()
|
94 |
. ' FROM ' . $c2
|
95 |
. ' WHERE ' . $this->getLocal()
|
96 |
. ' = ?';
|
97 |
}
|
98 |
}
|
99 |
*/
|
100 |
|
101 |
public function fetchRelatedFor(Doctrine_Record $record)
|
102 |
{
|
103 |
$id = $record->getIncremented();
|
104 |
|
105 |
|
106 |
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
|
107 |
return new Doctrine_Collection($this->getTable());
|
108 |
} else {
|
109 |
$q = new Doctrine_RawSql();
|
110 |
|
111 |
$assocTable = $this->getAssociationFactory()->getTableName();
|
112 |
$tableName = $record->getTable()->getTableName();
|
113 |
$identifier = $record->getTable()->getIdentifier();
|
114 |
|
115 |
$sub = 'SELECT ' . $this->getForeign()
|
116 |
. ' FROM ' . $assocTable
|
117 |
. ' WHERE ' . $this->getLocal()
|
118 |
. ' = ?';
|
119 |
|
120 |
$condition[] = $tableName . '.' . $identifier . ' IN (' . $sub . ')';
|
121 |
$joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getForeign();
|
122 |
|
123 |
if ($this->definition['equal']) {
|
124 |
$sub2 = 'SELECT ' . $this->getLocal()
|
125 |
. ' FROM ' . $assocTable
|
126 |
. ' WHERE ' . $this->getForeign()
|
127 |
. ' = ?';
|
128 |
|
129 |
$condition[] = $tableName . '.' . $identifier . ' IN (' . $sub2 . ')';
|
130 |
$joinCondition[] = $tableName . '.' . $identifier . ' = ' . $assocTable . '.' . $this->getLocal();
|
131 |
}
|
132 |
$q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
|
133 |
->from($tableName . ' INNER JOIN ' . $assocTable . ' ON ' . implode(' OR ', $joinCondition))
|
134 |
->where(implode(' OR ', $condition));
|
135 |
$q->addComponent($tableName, $record->getTable()->getComponentName());
|
136 |
$q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
|
137 |
|
138 |
$params = ($this->definition['equal']) ? array($id, $id) : array($id);
|
139 |
|
140 |
return $q->execute($params);
|
141 |
}
|
142 |
}
|
143 |
} |