get id exception

This commit is contained in:
Yuri Kuznetsov
2022-03-21 11:34:48 +02:00
parent fa02291a1a
commit 3ef6de205f
24 changed files with 117 additions and 70 deletions
+2 -10
View File
@@ -198,11 +198,7 @@ class AclManager
protected function getTable(User $user): Table
{
$key = $user->getId();
if (!$key) {
$key = spl_object_hash($user);
}
$key = $user->hasId() ? $user->getId() : spl_object_hash($user);
if (!array_key_exists($key, $this->tableHashMap)) {
$this->tableHashMap[$key] = $this->tableFactory->create($user);
@@ -213,11 +209,7 @@ class AclManager
protected function getMap(User $user): Map
{
$key = $user->getId();
if (!$key) {
$key = spl_object_hash($user);
}
$key = $user->hasId() ? $user->getId() : spl_object_hash($user);
if (!array_key_exists($key, $this->mapHashMap)) {
$this->mapHashMap[$key] = $this->mapFactory->create($user, $this->getTable($user));
+2 -2
View File
@@ -108,7 +108,7 @@ class Finder
{
$entityType = $entity->getEntityType();
if ($entity->getId()) {
if ($entity->hasId()) {
$where = Cond::and(
$where,
Cond::notEqual(
@@ -136,7 +136,7 @@ class Finder
{
$entityType = $entity->getEntityType();
if ($entity->getId()) {
if ($entity->hasId()) {
$where = Cond::and(
$where,
Cond::notEqual(
+1 -1
View File
@@ -274,7 +274,7 @@ class Htmlizer
$relationList = $entity->getRelationList();
if (!$skipLinks && $level === 0 && $this->entityManager && $entity->getId()) {
if (!$skipLinks && $level === 0 && $this->entityManager && $entity->hasId()) {
foreach ($relationList as $relation) {
$collection = null;
@@ -162,7 +162,7 @@ class BeforeFetch implements BeforeFetchInterface
if (
$campaignId &&
$target &&
$target->getId()
$target->hasId()
) {
$this->getCampaignService()
->logBounced(
+2 -10
View File
@@ -109,11 +109,7 @@ class AclManager extends InternalAclManager
protected function getTable(User $user): TableBase
{
$key = $user->getId();
if (!$key) {
$key = spl_object_hash($user);
}
$key = $user->hasId() ? $user->getId() : spl_object_hash($user);
if (!array_key_exists($key, $this->tableHashMap)) {
$this->tableHashMap[$key] = $this->portalTableFactory->create($user, $this->getPortal());
@@ -124,11 +120,7 @@ class AclManager extends InternalAclManager
protected function getMap(User $user): Map
{
$key = $user->getId();
if (!$key) {
$key = spl_object_hash($user);
}
$key = $user->hasId() ? $user->getId() : spl_object_hash($user);
if (!array_key_exists($key, $this->mapHashMap)) {
/** @var Table */
@@ -69,7 +69,7 @@ class UserAclManagerProvider
*/
public function get(User $user): AclManager
{
$key = $user->getId() ?? spl_object_hash($user);
$key = $user->hasId() ? $user->getId() : spl_object_hash($user);
if (!isset($this->map[$key])) {
$this->map[$key] = $this->load($user);
@@ -304,7 +304,7 @@ class Campaign extends Record implements
if ($queueItem) {
$massEmail = $this->entityManager->getEntity('MassEmail', $queueItem->get('massEmailId'));
if ($massEmail && $massEmail->getId()) {
if ($massEmail && $massEmail->hasId()) {
$logRecord = $this->entityManager->getEntity('CampaignLogRecord');
$logRecord->set([
@@ -409,7 +409,7 @@ class Processor
if (
!$target ||
!$target->getId() ||
!$target->hasId() ||
!$emailAddress
) {
$queueItem->set('status', EmailQueueItem::STATUS_FAILED);
+18 -2
View File
@@ -130,9 +130,25 @@ class BaseEntity implements Entity
/**
* Get an entity ID.
*/
public function getId(): ?string
public function getId(): string
{
return $this->get('id');
/** @var ?string */
$id = $this->get('id');
if ($id === null) {
throw new RuntimeException("Entity ID is not set.");
}
if ($id === '') {
throw new RuntimeException("Entity ID is empty.");
}
return $id;
}
public function hasId(): bool
{
return $this->id !== null;
}
/**
+10 -2
View File
@@ -60,8 +60,16 @@ interface Entity
/**
* Get an entity ID.
*
* @return non-empty-string
* @throws \RuntimeException If an ID is not set.
*/
public function getId(): ?string;
public function getId(): string;
/**
* Whether an ID is set.
*/
public function hasId(): bool;
/**
* Reset all attributes (empty an entity).
@@ -73,7 +81,7 @@ interface Entity
*
* Two usage options:
* * `set(string $attribute, mixed $value)`
* * `set(array|object $valueMap)`
* * `set(array|stdClass $valueMap)`
*
* @param string|stdClass|array<string,mixed> $attribute
* @param mixed $value
+1 -1
View File
@@ -346,7 +346,7 @@ class EntityManager
throw new RuntimeException("Can't refresh a new entity.");
}
if ($entity->getId() === null) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't refresh an entity w/o ID.");
}
+5 -3
View File
@@ -700,10 +700,12 @@ class BaseMapper implements RDBMapper
{
$params = $select->getRaw();
$id = $entity->getId();
if (!$entity->hasId()) {
throw new RuntimeException("Entity w/o ID.");
}
if (empty($id) || empty($relationName)) {
throw new RuntimeException("Can't mass relate on empty ID or relation name.");
if (empty($relationName)) {
throw new RuntimeException("Empty relation name.");
}
$relType = $entity->getRelationType($relationName);
@@ -75,7 +75,7 @@ class RDBRelation
$this->entity = $entity;
$this->hookMediator = $hookMediator;
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't use an entity w/o ID.");
}
@@ -363,7 +363,7 @@ class RDBRelation
throw new RuntimeException("Entity type doesn't match an entity type of the relation.");
}
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't use an entity w/o ID.");
}
}
@@ -375,7 +375,7 @@ class RDBRelation
*/
public function isRelated(Entity $entity): bool
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't use an entity w/o ID.");
}
@@ -580,12 +580,12 @@ class RDBRelation
throw new RuntimeException("Can't update not many-to-many relation.");
}
$id = $entity->getId();
if ($id === null) {
if (!$entity->hasId()) {
throw new RuntimeException("Entity w/o ID.");
}
$id = $entity->getId();
$this->getMapper()->updateRelationColumns($this->entity, $this->relationName, $id, $columnData);
}
@@ -602,12 +602,12 @@ class RDBRelation
throw new RuntimeException("Can't get a column of not many-to-many relation.");
}
$id = $entity->getId();
if ($id === null) {
if (!$entity->hasId()) {
throw new RuntimeException("Entity w/o ID.");
}
$id = $entity->getId();
return $this->getMapper()->getRelationColumn($this->entity, $this->relationName, $id, $column);
}
@@ -306,7 +306,7 @@ class RDBRepository implements Repository
throw new RuntimeException("Not supported entity type.");
}
if (!$entity->getId()) {
if (!$entity->hasId()) {
return null;
}
@@ -373,7 +373,7 @@ class RDBRepository implements Repository
throw new RuntimeException("Not supported entity type.");
}
if (!$entity->getId()) {
if (!$entity->hasId()) {
return 0;
}
@@ -484,7 +484,7 @@ class RDBRepository implements Repository
*/
public function isRelated(Entity $entity, string $relationName, $foreign): bool
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
return false;
}
@@ -495,6 +495,10 @@ class RDBRepository implements Repository
/** @var mixed $foreign */
if ($foreign instanceof Entity) {
if (!$foreign->hasId()) {
return false;
}
$id = $foreign->getId();
}
else if (is_string($foreign)) {
@@ -535,11 +539,11 @@ class RDBRepository implements Repository
*/
public function relate(Entity $entity, string $relationName, $foreign, $columnData = null, array $options = [])
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't relate an entity w/o ID.");
}
if (! $foreign instanceof Entity && !is_string($foreign)) {
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad 'foreign' value.");
}
@@ -598,11 +602,11 @@ class RDBRepository implements Repository
*/
public function unrelate(Entity $entity, string $relationName, $foreign, array $options = [])
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't unrelate an entity w/o ID.");
}
if (! $foreign instanceof Entity && !is_string($foreign)) {
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad foreign value.");
}
@@ -710,11 +714,11 @@ class RDBRepository implements Repository
*/
public function updateRelation(Entity $entity, string $relationName, $foreign, $columnData)
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't update a relation for an entity w/o ID.");
}
if (! $foreign instanceof Entity && !is_string($foreign)) {
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad foreign value.");
}
@@ -743,7 +747,7 @@ class RDBRepository implements Repository
*/
public function massRelate(Entity $entity, string $relationName, array $params = [], array $options = [])
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new RuntimeException("Can't related an entity w/o ID.");
}
+1 -1
View File
@@ -132,7 +132,7 @@ class ArrayValue extends Database
public function deleteEntityAttribute(CoreEntity $entity, string $attribute): void
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
throw new Error("ArrayValue: Can't delete {$attribute} w/o id given.");
}
@@ -115,6 +115,10 @@ class EmailAddress extends \Espo\Core\Repositories\Database implements
*/
public function getEmailAddressData(Entity $entity): array
{
if (!$entity->hasId()) {
return [];
}
$dataList = [];
$emailAddressList = $this
@@ -105,6 +105,10 @@ class PhoneNumber extends Database implements
*/
public function getPhoneNumberData(Entity $entity): array
{
if (!$entity->hasId()) {
return [];
}
$dataList = [];
$numberList = $this
@@ -237,7 +237,7 @@ class Preferences implements Repository,
public function save(Entity $entity, array $options = []): void
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
return;
}
@@ -293,7 +293,7 @@ class Preferences implements Repository,
public function remove(Entity $entity, array $options = []): void
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
return;
}
+1 -1
View File
@@ -392,7 +392,7 @@ class Stream
public function unfollowAllUsersFromEntity(Entity $entity): void
{
if (!$entity->getId()) {
if (!$entity->hasId()) {
return;
}
@@ -277,7 +277,7 @@ class Processor
$text = str_replace('{' . $type . '.' . $variableName . '}', $value, $text);
}
if (!$skipLinks && $entity->getId()) {
if (!$skipLinks && $entity->hasId()) {
$text = $this->processLinks(
$type,
$entity,
+1 -1
View File
@@ -532,7 +532,7 @@ class Import
}
}
if ($entity->getId()) {
if ($entity->hasId()) {
$this->entityManager
->getRDBRepository($entity->getEntityType())
->deleteFromDb($entity->getId(), true);
@@ -352,7 +352,7 @@ class LeadCapture
}
}
if ($toRelateLead && $targetLead->getId()) {
if ($toRelateLead && $targetLead->hasId()) {
$this->entityManager
->getRDBRepository('Lead')
->relate($targetLead, 'targetLists', $leadCapture->get('targetListId'), [
@@ -596,19 +596,12 @@ class HookProcessor
$audited = $this->metadata->get(['entityDefs', $entityType, 'links', $link, 'audited']);
$auditedForeign = $this->metadata->get(['entityDefs', $foreignEntityType, 'links', $foreignLink, 'audited']);
$id = $entity->getId();
$foreignId = $foreignEntity->getId();
if (!$id || !$foreignId) {
return;
}
if ($audited) {
$this->service->noteRelate($foreignEntity, $entityType, $id);
$this->service->noteRelate($foreignEntity, $entityType, $entity->getId());
}
if ($auditedForeign) {
$this->service->noteRelate($entity, $foreignEntity->getEntityType(), $foreignId);
$this->service->noteRelate($entity, $foreignEntity->getEntityType(), $foreignEntity->getId());
}
}
+32
View File
@@ -74,6 +74,9 @@ class EntityTest extends \PHPUnit\Framework\TestCase
{
}
/**
* @return \Espo\ORM\Entity
*/
protected function createEntity(string $entityType, ?string $className = null)
{
$defs = $this->metadata->get($entityType);
@@ -348,4 +351,33 @@ class EntityTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(1, $entity->get('int'));
}
public function testHasId(): void
{
$entity = $this->createEntity('Test');
$this->assertFalse($entity->hasId());
$entity->set('id', '1');
$this->assertTrue($entity->hasId());
}
public function testGetId1(): void
{
$entity = $this->createEntity('Test');
$entity->set('id', '1');
$this->assertEquals('1', $entity->getId());
}
public function testGetIdEmpty(): void
{
$entity = $this->createEntity('Test');
$this->expectException(\RuntimeException::class);
$entity->getId();
}
}