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