From 070d375a6c9c30ae1736b83598d5b5f395d2f599 Mon Sep 17 00:00:00 2001 From: Daniel Corn Date: Thu, 15 Nov 2018 11:54:28 +0100 Subject: [PATCH] Add `isModelNew()` to allow overriding the check for new instances --- Classes/DataProvider/DataProvider.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Classes/DataProvider/DataProvider.php b/Classes/DataProvider/DataProvider.php index 325e3dde..de70aa02 100644 --- a/Classes/DataProvider/DataProvider.php +++ b/Classes/DataProvider/DataProvider.php @@ -10,6 +10,7 @@ use Psr\Log\LogLevel; use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface; use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; use TYPO3\CMS\Extbase\Property\Exception as ExtbaseException; @@ -165,7 +166,7 @@ public function getModelProperty($model, $propertyKey) public function saveModel($model, ResourceType $resourceType) { $repository = $this->getRepositoryForResourceType($resourceType); - if ($model->_isNew()) { + if ($this->isModelNew($model)) { $repository->add($model); } else { $repository->update($model); @@ -329,4 +330,22 @@ protected function logException(\Exception $exception) $message = 'Uncaught exception #' . $exception->getCode() . ': ' . $exception->getMessage(); $this->getLogger()->log(LogLevel::ERROR, $message, ['exception' => $exception]); } + + /** + * Return if the given instance is not yet stored in the database + * + * @param object|DomainObjectInterface $model + * @return bool + */ + protected function isModelNew($model) + { + if ($model instanceof DomainObjectInterface) { + return $model->_isNew(); + } + if (is_callable($model, 'getUid')) { + return $model->getUid() === null; + } + + return true; + } }