Skip to content

Commit

Permalink
[BUGFIX] Reuse entities of overridden classes in persistence session
Browse files Browse the repository at this point in the history
When saving and retrieving object from the persistence session,
implementation class names set via \TYPO3\CMS\Extbase\Object\Container
are now respected by storing and retrieving them with that overridden
class name.

Resolves: #77617
Releases: master, 9.5
Change-Id: I887cb164328afe789dc274c92c8f8774b2098a40
Reviewed-on: https://review.typo3.org/58959
Tested-by: TYPO3com <noreply@typo3.com>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Tested-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
  • Loading branch information
MK-42 authored and maddy2101 committed Feb 1, 2019
1 parent 265ced7 commit 4c0fd85
Show file tree
Hide file tree
Showing 21 changed files with 373 additions and 17 deletions.
28 changes: 23 additions & 5 deletions typo3/sysext/extbase/Classes/Persistence/Generic/Session.php
Expand Up @@ -22,6 +22,11 @@
*/
class Session implements \TYPO3\CMS\Core\SingletonInterface
{
/**
* @var \TYPO3\CMS\Extbase\Object\Container\Container
*/
protected $objectContainer;

/**
* Reconstituted objects
*
Expand All @@ -42,8 +47,9 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
/**
* Constructs a new Session
*/
public function __construct()
public function __construct(\TYPO3\CMS\Extbase\Object\Container\Container $container)
{
$this->objectContainer = $container;
$this->reconstitutedEntities = new ObjectStorage();
$this->objectMap = new ObjectStorage();
}
Expand Down Expand Up @@ -128,7 +134,7 @@ public function hasObject($object)
*/
public function hasIdentifier($identifier, $className)
{
return isset($this->identifierMap[strtolower($className)][$identifier]);
return isset($this->identifierMap[$this->getClassIdentifier($className)][$identifier]);
}

/**
Expand All @@ -140,7 +146,7 @@ public function hasIdentifier($identifier, $className)
*/
public function getObjectByIdentifier($identifier, $className)
{
return $this->identifierMap[strtolower($className)][$identifier];
return $this->identifierMap[$this->getClassIdentifier($className)][$identifier];
}

/**
Expand Down Expand Up @@ -168,7 +174,7 @@ public function getIdentifierByObject($object)
public function registerObject($object, $identifier)
{
$this->objectMap[$object] = $identifier;
$this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
$this->identifierMap[$this->getClassIdentifier(get_class($object))][$identifier] = $object;
}

/**
Expand All @@ -178,7 +184,7 @@ public function registerObject($object, $identifier)
*/
public function unregisterObject($object)
{
unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
unset($this->identifierMap[$this->getClassIdentifier(get_class($object))][$this->objectMap[$object]]);
$this->objectMap->detach($object);
}

Expand All @@ -192,4 +198,16 @@ public function destroy()
$this->objectMap = new ObjectStorage();
$this->reconstitutedEntities = new ObjectStorage();
}

/**
* Objects are stored in the cache with their implementation class name
* to allow reusing instances of different classes that point to the same implementation
*
* @param string $className
* @return string a unique class identifier respecting configured implementation class names
*/
protected function getClassIdentifier($className): string
{
return strtolower($this->objectContainer->getImplementationClassName($className));
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types = 1);

namespace ExtbaseTeam\A\Domain\Model;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* Class ExtbaseTeam\A\Domain\Model\A
*/
class A extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* @var string
*/
protected $a;

/**
* @return string
*/
public function getA(): string
{
return $this->a;
}

/**
* @param string $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);

namespace ExtbaseTeam\A\Domain\Model;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Extbase\Persistence\Repository;

/**
* Class ExtbaseTeam\A\Domain\Model\ARepository
*/
class ARepository extends Repository
{
}
@@ -0,0 +1,25 @@
<?php
return [
'ctrl' => [
'label' => 'uid',
'title' => 'A',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
],
'interface' => [
'showRecordFieldList' => 'title'
],
'columns' => [
'a' => [
'label' => 'a',
'config' => [
'type' => 'input',
'size' => 25,
'max' => 255,
]
],
],
'types' => [
'1' => ['showitem' => '--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,a']
],
];
@@ -0,0 +1,21 @@
<?php
$EM_CONF[$_EXTKEY] = [
'title' => '',
'description' => '',
'category' => 'example',
'author' => '',
'author_company' => '',
'author_email' => '',
'state' => 'stable',
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 1,
'version' => '10.0.0',
'constraints' => [
'depends' => [
'typo3' => '10.0.0',
],
'conflicts' => [],
'suggests' => [],
],
];
@@ -0,0 +1,2 @@
<?php
defined('TYPO3_MODE') or die();
@@ -0,0 +1,2 @@
<?php
defined('TYPO3_MODE') or die();
@@ -0,0 +1,14 @@
--
-- Table structure for table 'tx_a_domain_model_a'
--
CREATE TABLE tx_a_domain_model_a (
uid int(11) NOT NULL auto_increment,
pid int(11) DEFAULT '0' NOT NULL,
tstamp int(11) DEFAULT '0' NOT NULL,
crdate int(11) DEFAULT '0' NOT NULL,

a varchar(255) DEFAULT '' NOT NULL,

PRIMARY KEY (uid),
KEY parent (pid)
);
@@ -0,0 +1,44 @@
<?php
declare(strict_types = 1);

namespace ExtbaseTeam\B\Domain\Model;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* Class ExtbaseTeam\B\Domain\Model\B
*/
class B extends \ExtbaseTeam\A\Domain\Model\A
{
/**
* @var string
*/
protected $b;

/**
* @return string
*/
public function getB(): string
{
return $this->b;
}

/**
* @param string $b
*/
public function setB(string $b): void
{
$this->b = $b;
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);

namespace ExtbaseTeam\B\Domain\Model;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Extbase\Persistence\Repository;

/**
* Class ExtbaseTeam\B\Domain\Model\BRepository
*/
class BRepository extends Repository
{
}
@@ -0,0 +1,17 @@
<?php

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tx_a_domain_model_a',
[
'b' => [
'label' => 'b',
'config' => [
'type' => 'input',
'size' => 25,
'max' => 255,
]
],
]
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('tx_a_domain_model_a', 'b');
@@ -0,0 +1,21 @@
<?php
$EM_CONF[$_EXTKEY] = [
'title' => '',
'description' => '',
'category' => 'example',
'author' => '',
'author_company' => '',
'author_email' => '',
'state' => 'stable',
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 1,
'version' => '10.0.0',
'constraints' => [
'depends' => [
'typo3' => '10.0.0',
],
'conflicts' => [],
'suggests' => [],
],
];
@@ -0,0 +1,8 @@
<?php
defined('TYPO3_MODE') or die();

\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class)
->registerImplementation(
\ExtbaseTeam\A\Domain\Model\A::class,
\ExtbaseTeam\B\Domain\Model\B::class
);
@@ -0,0 +1,2 @@
<?php
defined('TYPO3_MODE') or die();
@@ -0,0 +1,6 @@
--
-- Table structure for table 'tx_a_domain_model_a'
--
CREATE TABLE tx_a_domain_model_a (
b varchar(255) DEFAULT '' NOT NULL
);
@@ -0,0 +1,9 @@
config.tx_extbase.persistence {
classes {
ExtbaseTeam\B\Domain\Model\B {
mapping {
tableName = tx_a_domain_model_a
}
}
}
}
@@ -0,0 +1,3 @@
"tx_a_domain_model_a",,,,,,
,"uid", "pid", "tstamp", "crdate", "a", "b"
,1, 0, 1546178428, 1546178428, "A", "B"

0 comments on commit 4c0fd85

Please sign in to comment.