1 |
<?php
|
2 |
/*
|
3 |
* $Id: Association.php 3209 2007-11-24 18:11:09Z romanb $
|
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');
|
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.phpdoctrine.org
|
32 |
* @since 1.0
|
33 |
* @version $Revision: 3209 $
|
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 |
public function getAssociationTable()
|
46 |
{
|
47 |
return $this->definition['refTable'];
|
48 |
}
|
49 |
|
50 |
/**
|
51 |
* getRelationDql
|
52 |
*
|
53 |
* @param integer $count
|
54 |
* @return string
|
55 |
*/
|
56 |
public function getRelationDql($count, $context = 'record')
|
57 |
{
|
58 |
$table = $this->definition['refTable'];
|
59 |
$component = $this->definition['refTable']->getComponentName();
|
60 |
|
61 |
switch ($context) {
|
62 |
case "record":
|
63 |
$sub = substr(str_repeat("?, ", $count),0,-2);
|
64 |
$dql = 'FROM ' . $this->getTable()->getComponentName();
|
65 |
$dql .= '.' . $component;
|
66 |
$dql .= ' WHERE ' . $this->getTable()->getComponentName()
|
67 |
. '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
|
68 |
break;
|
69 |
case "collection":
|
70 |
$sub = substr(str_repeat("?, ", $count),0,-2);
|
71 |
$dql = 'FROM ' . $component . '.' . $this->getTable()->getComponentName();
|
72 |
$dql .= ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
|
73 |
break;
|
74 |
}
|
75 |
|
76 |
return $dql;
|
77 |
}
|
78 |
|
79 |
/**
|
80 |
* fetchRelatedFor
|
81 |
*
|
82 |
* fetches a component related to given record
|
83 |
*
|
84 |
* @param Doctrine_Record $record
|
85 |
* @return Doctrine_Record|Doctrine_Collection
|
86 |
*/
|
87 |
public function fetchRelatedFor(Doctrine_Record $record)
|
88 |
{
|
89 |
$id = $record->getIncremented();
|
90 |
if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {
|
91 |
$coll = new Doctrine_Collection($this->getTable());
|
92 |
} else {
|
93 |
$coll = Doctrine_Query::create()->query($this->getRelationDql(1), array($id));
|
94 |
}
|
95 |
$coll->setReference($record, $this);
|
96 |
return $coll;
|
97 |
}
|
98 |
} |