2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
33 * @subpackage Persistence
34 * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
36 // TODO Implement support for CountableInterface
37 class Tx_Extbase_Persistence_LazyLoadingProxy
{
40 * @var Tx_Extbase_Persistence_QueryFactoryInterface
42 protected $queryFactory;
45 * The object this property is contained in.
49 private $parentObject;
52 * The name of the property represented by this proxy.
56 private $propertyName;
60 * @var Tx_Extbase_Persistence_Mapper_DataMap
65 * Constructs this proxy instance.
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
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;
80 * Populate this proxy by asking the $population closure.
82 * @return object The instance (hopefully) returned
85 public function _loadRealInstance() {
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(), FALSE);
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(), FALSE);
96 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
97 if (isset($parentKeyFieldName)) {
98 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $this->parentObject
->getUid()))->execute();
100 $propertyValue = $row[$propertyName];
101 $objects = $query->matching($query->withUid((int)$propertyValue))->execute();
103 foreach ($objects as $object) {
104 $objectStorage->attach($object);
106 $result = $objectStorage;
107 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap
::RELATION_HAS_AND_BELONGS_TO_MANY
) {
108 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
109 $relationTableName = $columnMap->getRelationTableName();
110 $left = $this->QOMFactory
->selector($relationTableName);
111 $childTableName = $columnMap->getChildTableName();
112 $right = $this->QOMFactory
->selector($childTableName);
113 $joinCondition = $this->QOMFactory
->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
114 $source = $this->QOMFactory
->join(
117 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_JOIN_TYPE_INNER
,
120 $query = $this->queryFactory
->create($columnMap->getChildClassName(), FALSE);
121 $query->setSource($source);
122 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $this->parentObject
->getUid()))->execute();
123 foreach ($objects as $object) {
124 $objectStorage->attach($object);
126 $result = $objectStorage;
128 $this->parentObject
->_setProperty($this->propertyName
, $result);
129 $this->parentObject
->_memorizeCleanState($this->propertyName
);
134 * Magic method call implementation.
136 * @param string $methodName The name of the property to get
137 * @param array $arguments The arguments given to the call
141 public function __call($methodName, $arguments) {
142 $realInstance = $this->_loadRealInstance();
143 return call_user_func_array(array($realInstance, $methodName), $arguments);
147 * Magic get call implementation.
149 * @param string $propertyName The name of the property to get
153 public function __get($propertyName) {
154 $realInstance = $this->_loadRealInstance();
155 return $realInstance->$propertyName;
159 * Magic set call implementation.
161 * @param string $propertyName The name of the property to set
162 * @param mixed $value The value for the property to set
166 public function __set($propertyName, $value) {
167 $realInstance = $this->_loadRealInstance();
168 $realInstance->$propertyName = $value;
172 * Magic isset call implementation.
174 * @param string $propertyName The name of the property to check
178 public function __isset($propertyName) {
179 $realInstance = $this->_loadRealInstance();
180 return isset($realInstance->$propertyName);
184 * Magic unset call implementation.
186 * @param string $propertyName The name of the property to unset
190 public function __unset($propertyName) {
191 $realInstance = $this->_loadRealInstance();
192 unset($realInstance->$propertyName);