refresh entity
This commit is contained in:
@@ -594,7 +594,7 @@ class BaseEntity implements Entity
|
||||
}
|
||||
|
||||
/**
|
||||
* Set as not new. Meaning an entity is fetched or already saved.
|
||||
* Set as not new. Meaning the entity is fetched or already saved.
|
||||
*/
|
||||
public function setAsNotNew(): void
|
||||
{
|
||||
|
||||
@@ -138,12 +138,18 @@ interface Entity
|
||||
public function isNew(): bool;
|
||||
|
||||
/**
|
||||
* Whether is fetched from DB.
|
||||
* Set an entity as fetched. All current attribute values will be set as those that are fetched
|
||||
* from the database.
|
||||
*/
|
||||
public function setAsFetched(): void;
|
||||
|
||||
/**
|
||||
* Whether is fetched from the database.
|
||||
*/
|
||||
public function isFetched(): bool;
|
||||
|
||||
/**
|
||||
* Whether an attribute was changed (since syncing with DB).
|
||||
* Whether an attribute was changed (since syncing with the database).
|
||||
*/
|
||||
public function isAttributeChanged(string $name): bool;
|
||||
|
||||
|
||||
@@ -292,6 +292,32 @@ class EntityManager
|
||||
$this->getRepository($entityType)->remove($entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh an entity from the database, overwriting made changes, if any.
|
||||
* Can be used to fetch attributes that were not fetched initially.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function refreshEntity(Entity $entity): void
|
||||
{
|
||||
if ($entity->isNew()) {
|
||||
throw new RuntimeException("Can't refresh a new entity.");
|
||||
}
|
||||
|
||||
if ($entity->getId() === null) {
|
||||
throw new RuntimeException("Can't refresh an entity w/o ID.");
|
||||
}
|
||||
|
||||
$fetchedEntity = $this->getEntity($entity->getEntityType(), $entity->getId());
|
||||
|
||||
if (!$fetchedEntity) {
|
||||
throw new RuntimeException("Can't refresh a non-existent entity.");
|
||||
}
|
||||
|
||||
$entity->set($fetchedEntity->getValueMap());
|
||||
$entity->setAsFetched();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create entity (and store to database).
|
||||
*
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\integration\Espo\ORM;
|
||||
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
class EntityManager extends \tests\integration\Core\BaseTestCase
|
||||
{
|
||||
public function testRefreshEntity(): void
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
|
||||
/** @var EntityManager $em */
|
||||
$em = $app->getContainer()->get('entityManager');
|
||||
|
||||
$entity = $em->createEntity('Account', [
|
||||
'name' => 'Test',
|
||||
]);
|
||||
|
||||
$entity->set('name', 'Hello');
|
||||
|
||||
$em->refreshEntity($entity);
|
||||
|
||||
$this->assertEquals('Test', $entity->get('name'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user