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;
59 * The raw field value.
67 * @var Tx_Extbase_Persistence_Mapper_ColumnMap
72 * Constructs this proxy instance.
74 * @param object $parentObject The object instance this proxy is part of
75 * @param string $propertyName The name of the proxied property in it's parent
76 * @param mixed $fieldValue The raw field value.
77 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
80 public function __construct($parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap
$columnMap) {
81 $this->queryFactory
= t3lib_div
::makeInstance('Tx_Extbase_Persistence_QueryFactory');
82 $this->parentObject
= $parentObject;
83 $this->propertyName
= $propertyName;
84 $this->fieldValue
= $fieldValue;
85 $this->columnMap
= $columnMap;
89 * Populate this proxy by asking the $population closure.
91 * @return object The instance (hopefully) returned
94 public function _loadRealInstance() {
95 $dataMapper = Tx_Extbase_Dispatcher
::getPersistenceManager()->getBackend()->getDataMapper();
96 $result = $dataMapper->fetchRelatedObjects($this->parentObject
, $this->propertyName
, $this->fieldValue
, $this->columnMap
);
97 $this->parentObject
->_setProperty($this->propertyName
, $result);
98 $this->parentObject
->_memorizeCleanState($this->propertyName
);
103 * Magic method call implementation.
105 * @param string $methodName The name of the property to get
106 * @param array $arguments The arguments given to the call
110 public function __call($methodName, $arguments) {
111 $realInstance = $this->_loadRealInstance();
112 return call_user_func_array(array($realInstance, $methodName), $arguments);
116 * Magic get call implementation.
118 * @param string $propertyName The name of the property to get
122 public function __get($propertyName) {
123 $realInstance = $this->_loadRealInstance();
124 return $realInstance->$propertyName;
128 * Magic set call implementation.
130 * @param string $propertyName The name of the property to set
131 * @param mixed $value The value for the property to set
135 public function __set($propertyName, $value) {
136 $realInstance = $this->_loadRealInstance();
137 $realInstance->$propertyName = $value;
141 * Magic isset call implementation.
143 * @param string $propertyName The name of the property to check
147 public function __isset($propertyName) {
148 $realInstance = $this->_loadRealInstance();
149 return isset($realInstance->$propertyName);
153 * Magic unset call implementation.
155 * @param string $propertyName The name of the property to unset
159 public function __unset($propertyName) {
160 $realInstance = $this->_loadRealInstance();
161 unset($realInstance->$propertyName);