| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Association.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 this class takes care of association mapping
|
|
|
24 |
* (= many-to-many relationships, where the relationship is handled with an additional relational table
|
|
|
25 |
* which holds 2 foreign keys)
|
|
|
26 |
*
|
|
|
27 |
*
|
|
|
28 |
* @package Doctrine
|
|
|
29 |
* @subpackage Relation
|
|
|
30 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
31 |
* @link www.doctrine-project.org
|
|
|
32 |
* @since 1.0
|
|
|
33 |
* @version $Revision: 7490 $
|
|
|
34 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
35 |
*/
|
|
|
36 |
class Doctrine_Relation_Association extends Doctrine_Relation
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* @return Doctrine_Table
|
|
|
40 |
*/
|
|
|
41 |
public function getAssociationFactory()
|
|
|
42 |
{
|
|
|
43 |
return $this->definition['refTable'];
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function getAssociationTable()
|
|
|
47 |
{
|
|
|
48 |
return $this->definition['refTable'];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* getRelationDql
|
|
|
53 |
*
|
|
|
54 |
* @param integer $count
|
|
|
55 |
* @return string
|
|
|
56 |
*/
|
|
|
57 |
public function getRelationDql($count, $context = 'record')
|
|
|
58 |
{
|
|
|
59 |
$table = $this->definition['refTable'];
|
|
|
60 |
$component = $this->definition['refTable']->getComponentName();
|
|
|
61 |
|
|
|
62 |
switch ($context) {
|
|
|
63 |
case "record":
|
|
|
64 |
$sub = substr(str_repeat("?, ", $count),0,-2);
|
|
|
65 |
$dql = 'FROM ' . $this->getTable()->getComponentName();
|
|
|
66 |
$dql .= '.' . $component;
|
|
|
67 |
$dql .= ' WHERE ' . $this->getTable()->getComponentName()
|
|
|
68 |
. '.' . $component . '.' . $this->getLocalRefColumnName() . ' IN (' . $sub . ')';
|
|
|
69 |
$dql .= $this->getOrderBy($this->getTable()->getComponentName(), false);
|
|
|
70 |
break;
|
|
|
71 |
case "collection":
|
|
|
72 |
$sub = substr(str_repeat("?, ", $count),0,-2);
|
|
|
73 |
$dql = 'FROM ' . $component . '.' . $this->getTable()->getComponentName();
|
|
|
74 |
$dql .= ' WHERE ' . $component . '.' . $this->getLocalRefColumnName() . ' IN (' . $sub . ')';
|
|
|
75 |
$dql .= $this->getOrderBy($component, false);
|
|
|
76 |
break;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $dql;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* getLocalRefColumnName
|
|
|
84 |
* returns the column name of the local reference column
|
|
|
85 |
*/
|
|
|
86 |
final public function getLocalRefColumnName()
|
|
|
87 |
{
|
|
|
88 |
return $this->definition['refTable']->getColumnName($this->definition['local']);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* getLocalRefFieldName
|
|
|
93 |
* returns the field name of the local reference column
|
|
|
94 |
*/
|
|
|
95 |
final public function getLocalRefFieldName()
|
|
|
96 |
{
|
|
|
97 |
return $this->definition['refTable']->getFieldName($this->definition['local']);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* getForeignRefColumnName
|
|
|
102 |
* returns the column name of the foreign reference column
|
|
|
103 |
*/
|
|
|
104 |
final public function getForeignRefColumnName()
|
|
|
105 |
{
|
|
|
106 |
return $this->definition['refTable']->getColumnName($this->definition['foreign']);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* getForeignRefFieldName
|
|
|
111 |
* returns the field name of the foreign reference column
|
|
|
112 |
*/
|
|
|
113 |
final public function getForeignRefFieldName()
|
|
|
114 |
{
|
|
|
115 |
return $this->definition['refTable']->getFieldName($this->definition['foreign']);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* fetchRelatedFor
|
|
|
120 |
*
|
|
|
121 |
* fetches a component related to given record
|
|
|
122 |
*
|
|
|
123 |
* @param Doctrine_Record $record
|
|
|
124 |
* @return Doctrine_Record|Doctrine_Collection
|
|
|
125 |
*/
|
|
|
126 |
public function fetchRelatedFor(Doctrine_Record $record)
|
|
|
127 |
{
|
|
|
128 |
$id = $record->getIncremented();
|
|
|
129 |
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine_Core::ATTR_LOAD_REFERENCES)) {
|
|
|
130 |
$coll = Doctrine_Collection::create($this->getTable());
|
|
|
131 |
} else {
|
|
|
132 |
$coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id));
|
|
|
133 |
}
|
|
|
134 |
$coll->setReference($record, $this);
|
|
|
135 |
return $coll;
|
|
|
136 |
}
|
|
|
137 |
}
|