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
36 class Tx_Extbase_Persistence_LazyObjectStorage
extends Tx_Extbase_Persistence_ObjectStorage
implements Tx_Extbase_Persistence_LoadingStrategyInterface
{
39 * The object this property is contained in.
43 protected $parentObject;
46 * The name of the property represented by this proxy.
50 protected $propertyName;
53 * The raw field value.
57 protected $fieldValue;
63 protected $isInitialized = FALSE;
66 * Constructs this proxy instance.
68 * @param object $parentObject The object instance this proxy is part of
69 * @param string $propertyName The name of the proxied property in it's parent
70 * @param mixed $fieldValue The raw field value.
72 public function __construct($parentObject, $propertyName, $fieldValue) {
73 $this->parentObject
= $parentObject;
74 $this->propertyName
= $propertyName;
75 $this->fieldValue
= $fieldValue;
79 * This is a function lazy load implementation.
83 protected function initializeStorage() {
84 if (!$this->isInitialized
) {
85 $dataMapper = Tx_Extbase_Dispatcher
::getPersistenceManager()->getBackend()->getDataMapper();
86 $objects = $dataMapper->fetchRelated($this->parentObject
, $this->propertyName
, $this->fieldValue
, FALSE);
88 foreach ($objects as $object) {
89 $storage[spl_object_hash($object)] = $object;
91 $this->storage
= $storage;
92 $this->parentObject
->_memorizeCleanState($this->propertyName
);
93 $this->isInitialized
= TRUE;
98 * Counts the elements in the storage array
102 public function count() {
103 $dataMapper = Tx_Extbase_Dispatcher
::getPersistenceManager()->getBackend()->getDataMapper();
104 $columnMap = $dataMapper->getDataMap(get_class($this->parentObject
))->getColumnMap($this->propertyName
);
105 $numberOfElements = NULL;
106 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap
::RELATION_HAS_MANY
) {
107 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
108 $dataMapper = Tx_Extbase_Dispatcher
::getPersistenceManager()->getBackend()->getDataMapper();
109 $numberOfElements = $dataMapper->countRelated($this->parentObject
, $this->propertyName
, $this->fieldValue
);
111 $this->initializeStorage();
112 $numberOfElements = count($this->storage
);
114 if (is_null($numberOfElements)) {
115 throw new Tx_Extbase_Persistence_Exception('The number of elements could not be determined.', 1252514486);
117 return $numberOfElements;