[+FEATURE] Extbase (Persistence): Implemented support for mm_opposite_field now....
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyLoadingProxy.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
35 */
36 // TODO Implement support for CountableInterface
37 class Tx_Extbase_Persistence_LazyLoadingProxy {
38
39 /**
40 * @var Tx_Extbase_Persistence_QueryFactoryInterface
41 */
42 protected $queryFactory;
43
44 /**
45 * The object this property is contained in.
46 *
47 * @var object
48 */
49 private $parentObject;
50
51 /**
52 * The name of the property represented by this proxy.
53 *
54 * @var string
55 */
56 private $propertyName;
57
58 /**
59 *
60 * @var Tx_Extbase_Persistence_Mapper_DataMap
61 */
62 private $dataMap;
63
64 /**
65 * Constructs this proxy instance.
66 *
67 * @param object $parentObject The object instance this proxy is part of
68 * @param string $propertyName The name of the proxied property in it's parent
69 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
70 * @internal
71 */
72 public function __construct($parentObject, $propertyName, Tx_Extbase_Persistence_Mapper_DataMap $dataMap) {
73 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
74 $this->parentObject = $parentObject;
75 $this->propertyName = $propertyName;
76 $this->dataMap = $dataMap;
77 }
78
79 /**
80 * Populate this proxy by asking the $population closure.
81 *
82 * @return object The instance (hopefully) returned
83 * @internal
84 */
85 public function _loadRealInstance() {
86 $result = NULL;
87 $columnMap = $this->dataMap->getColumnMap($this->propertyName);
88 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
89 // TODO This if statement should be further encapsulated to follow the DRY principle (see Data Mapper)
90 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE) {
91 $query = $this->queryFactory->create($columnMap->getChildClassName());
92 $result = current($query->matching($query->withUid($row[$columnMap->getColumnName()]))->execute());
93 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
94 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
95 $query = $this->queryFactory->create($columnMap->getChildClassName());
96 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $this->parentObject->getUid()))->execute();
97 foreach ($objects as $object) {
98 $objectStorage->attach($object);
99 }
100 $result = $objectStorage;
101 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
102 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
103 $relationTableName = $columnMap->getRelationTableName();
104 $left = $this->QOMFactory->selector($relationTableName);
105 $childTableName = $columnMap->getChildTableName();
106 $right = $this->QOMFactory->selector($childTableName);
107 $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
108 $source = $this->QOMFactory->join(
109 $left,
110 $right,
111 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER,
112 $joinCondition
113 );
114 $query = $this->queryFactory->create($columnMap->getChildClassName());
115 $query->setSource($source);
116 $objects = $query->matching($query->equals($columnMap->getChildKeyFieldName(), $this->parentObject->getUid()))->execute();
117 foreach ($objects as $object) {
118 $objectStorage->attach($object);
119 }
120 $result = $objectStorage;
121 }
122 $this->parentObject->_setProperty($this->propertyName, $result);
123 $this->parentObject->_memorizeCleanState($this->propertyName);
124 return $result;
125 }
126
127 /**
128 * Magic method call implementation.
129 *
130 * @param string $methodName The name of the property to get
131 * @param array $arguments The arguments given to the call
132 * @return mixed
133 * @internal
134 */
135 public function __call($methodName, $arguments) {
136 $realInstance = $this->_loadRealInstance();
137 return call_user_func_array(array($realInstance, $methodName), $arguments);
138 }
139
140 /**
141 * Magic get call implementation.
142 *
143 * @param string $propertyName The name of the property to get
144 * @return mixed
145 * @internal
146 */
147 public function __get($propertyName) {
148 $realInstance = $this->_loadRealInstance();
149 return $realInstance->$propertyName;
150 }
151
152 /**
153 * Magic set call implementation.
154 *
155 * @param string $propertyName The name of the property to set
156 * @param mixed $value The value for the property to set
157 * @return void
158 * @internal
159 */
160 public function __set($propertyName, $value) {
161 $realInstance = $this->_loadRealInstance();
162 $realInstance->$propertyName = $value;
163 }
164
165 /**
166 * Magic isset call implementation.
167 *
168 * @param string $propertyName The name of the property to check
169 * @return boolean
170 * @internal
171 */
172 public function __isset($propertyName) {
173 $realInstance = $this->_loadRealInstance();
174 return isset($realInstance->$propertyName);
175 }
176
177 /**
178 * Magic unset call implementation.
179 *
180 * @param string $propertyName The name of the property to unset
181 * @return void
182 * @internal
183 */
184 public function __unset($propertyName) {
185 $realInstance = $this->_loadRealInstance();
186 unset($realInstance->$propertyName);
187 }
188 }
189 ?>