Skip to content

Commit

Permalink
Add isModelNew() to allow overriding the check for new instances
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Nov 15, 2018
1 parent c8b8b7d commit 070d375
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Classes/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 070d375

Please sign in to comment.