Initial commit
This commit is contained in:
1758
application/Espo/ORM/Mapper/BaseMapper.php
Normal file
1758
application/Espo/ORM/Mapper/BaseMapper.php
Normal file
File diff suppressed because it is too large
Load Diff
156
application/Espo/ORM/Mapper/Helper.php
Normal file
156
application/Espo/ORM/Mapper/Helper.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Mapper;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Metadata;
|
||||
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use RuntimeException;
|
||||
|
||||
class Helper
|
||||
{
|
||||
public function __construct(private Metadata $metadata)
|
||||
{}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* key: string,
|
||||
* foreignKey: string,
|
||||
* foreignType?: string,
|
||||
* nearKey?: string,
|
||||
* distantKey?: string,
|
||||
* typeKey?: string,
|
||||
* }
|
||||
*/
|
||||
public function getRelationKeys(Entity $entity, string $relationName): array
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
$defs = $this->metadata->getDefs()
|
||||
->getEntity($entityType)
|
||||
->getRelation($relationName);
|
||||
|
||||
$type = $defs->getType();
|
||||
|
||||
switch ($type) {
|
||||
|
||||
case Entity::BELONGS_TO:
|
||||
$key = $defs->hasKey() ?
|
||||
$defs->getKey() :
|
||||
$relationName . 'Id';
|
||||
|
||||
$foreignKey = $defs->hasForeignKey() ?
|
||||
$defs->getForeignKey() :
|
||||
Attribute::ID;
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'foreignKey' => $foreignKey,
|
||||
];
|
||||
|
||||
case Entity::HAS_MANY:
|
||||
case Entity::HAS_ONE:
|
||||
$key = $defs->hasKey() ? $defs->getKey() : Attribute::ID;
|
||||
|
||||
$foreign = $defs->hasForeignRelationName() ?
|
||||
$defs->getForeignRelationName() :
|
||||
null;
|
||||
|
||||
$foreignKey = $defs->hasForeignKey() ?
|
||||
$defs->getForeignKey() :
|
||||
null;
|
||||
|
||||
if (!$foreignKey && $foreign) {
|
||||
$foreignKey = $foreign . 'Id';
|
||||
}
|
||||
|
||||
if (!$foreignKey) {
|
||||
$foreignKey = lcfirst($entity->getEntityType()) . 'Id';
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'foreignKey' => $foreignKey,
|
||||
];
|
||||
|
||||
case Entity::HAS_CHILDREN:
|
||||
$key = $defs->hasKey() ? $defs->getKey() : Attribute::ID;
|
||||
|
||||
$foreignKey = $defs->hasForeignKey() ?
|
||||
$defs->getForeignKey() :
|
||||
'parentId';
|
||||
|
||||
$foreignType = $defs->getParam('foreignType') ?? 'parentType';
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'foreignKey' => $foreignKey,
|
||||
'foreignType' => $foreignType,
|
||||
];
|
||||
|
||||
case Entity::MANY_MANY:
|
||||
$key = $defs->hasKey() ?
|
||||
$defs->getKey() :
|
||||
Attribute::ID;
|
||||
|
||||
$foreignKey = $defs->hasForeignKey() ?
|
||||
$defs->getForeignKey() :
|
||||
Attribute::ID;
|
||||
|
||||
$nearKey = $defs->hasMidKey() ?
|
||||
$defs->getMidKey() :
|
||||
lcfirst($entityType) . 'Id';
|
||||
|
||||
$distantKey = $defs->hasForeignMidKey() ?
|
||||
$defs->getForeignMidKey() :
|
||||
lcfirst($defs->getForeignEntityType()) . 'Id';
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'foreignKey' => $foreignKey,
|
||||
'nearKey' => $nearKey,
|
||||
'distantKey' => $distantKey,
|
||||
];
|
||||
|
||||
case Entity::BELONGS_TO_PARENT:
|
||||
$key = $relationName . 'Id';
|
||||
$typeKey = $relationName . 'Type';
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'typeKey' => $typeKey,
|
||||
'foreignKey' => Attribute::ID,
|
||||
];
|
||||
}
|
||||
|
||||
throw new RuntimeException("Relation type '{$type}' not supported for 'getKeys'.");
|
||||
}
|
||||
}
|
||||
83
application/Espo/ORM/Mapper/Mapper.php
Normal file
83
application/Espo/ORM/Mapper/Mapper.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Mapper;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Collection;
|
||||
use Espo\ORM\Query\Select;
|
||||
|
||||
interface Mapper
|
||||
{
|
||||
/**
|
||||
* Get a first entity from DB.
|
||||
*/
|
||||
public function selectOne(Select $select): ?Entity;
|
||||
|
||||
/**
|
||||
* Select entities from DB.
|
||||
*
|
||||
* @return Collection<Entity>
|
||||
*/
|
||||
public function select(Select $select): Collection;
|
||||
|
||||
/**
|
||||
* Get a number of records in DB.
|
||||
*/
|
||||
public function count(Select $select): int;
|
||||
|
||||
/**
|
||||
* Insert an entity into DB.
|
||||
*/
|
||||
public function insert(Entity $entity): void;
|
||||
|
||||
/**
|
||||
* Insert a collection into DB.
|
||||
*
|
||||
* @param Collection<Entity> $collection
|
||||
*/
|
||||
public function massInsert(Collection $collection): void;
|
||||
|
||||
/**
|
||||
* Update an entity in DB.
|
||||
*/
|
||||
public function update(Entity $entity): void;
|
||||
|
||||
/**
|
||||
* Delete an entity from DB or mark as deleted.
|
||||
*/
|
||||
public function delete(Entity $entity): void;
|
||||
|
||||
/**
|
||||
* Insert an entity into DB, on duplicate key update specified attributes.
|
||||
*
|
||||
* @param string[] $onDuplicateUpdateAttributeList
|
||||
*/
|
||||
public function insertOnDuplicateUpdate(Entity $entity, array $onDuplicateUpdateAttributeList): void;
|
||||
}
|
||||
35
application/Espo/ORM/Mapper/MapperFactory.php
Normal file
35
application/Espo/ORM/Mapper/MapperFactory.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Mapper;
|
||||
|
||||
interface MapperFactory
|
||||
{
|
||||
public function create(string $name): Mapper;
|
||||
}
|
||||
117
application/Espo/ORM/Mapper/RDBMapper.php
Normal file
117
application/Espo/ORM/Mapper/RDBMapper.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Mapper;
|
||||
|
||||
use Espo\ORM\Collection;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Query\Select;
|
||||
|
||||
interface RDBMapper extends Mapper
|
||||
{
|
||||
/**
|
||||
* Relate an entity with another entity.
|
||||
*
|
||||
* @param Entity $entity An entity.
|
||||
* @param string $relationName A relation name.
|
||||
* @param Entity $foreignEntity A foreign entity.
|
||||
* @param array<string, mixed>|null $columnData Column values.
|
||||
* @return bool True if the row was affected.
|
||||
*/
|
||||
public function relate(Entity $entity, string $relationName, Entity $foreignEntity, ?array $columnData): bool;
|
||||
|
||||
/**
|
||||
* Unrelate an entity from another entity.
|
||||
*
|
||||
* @param Entity $entity An entity.
|
||||
* @param string $relationName A relation name.
|
||||
* @param Entity $foreignEntity A foreign entity.
|
||||
*/
|
||||
public function unrelate(Entity $entity, string $relationName, Entity $foreignEntity): void;
|
||||
|
||||
/**
|
||||
* Relate an entity from another entity by a given ID.
|
||||
*
|
||||
* @param Entity $entity An entity.
|
||||
* @param string $relationName A relation name.
|
||||
* @param string $id A foreign ID.
|
||||
* @param array<string, mixed>|null $columnData Column values.
|
||||
*/
|
||||
public function relateById(Entity $entity, string $relationName, string $id, ?array $columnData = null): bool;
|
||||
|
||||
/**
|
||||
* Unrelate an entity from another entity by a given ID.
|
||||
*
|
||||
* @param Entity $entity An entity.
|
||||
* @param string $relationName A relation name.
|
||||
* @param string $id A foreign ID.
|
||||
*/
|
||||
public function unrelateById(Entity $entity, string $relationName, string $id): void;
|
||||
|
||||
/**
|
||||
* Mass relate.
|
||||
*/
|
||||
public function massRelate(Entity $entity, string $relationName, Select $select): void;
|
||||
|
||||
/**
|
||||
* Update relationship columns.
|
||||
*
|
||||
* @param array<string, mixed> $columnData
|
||||
*/
|
||||
public function updateRelationColumns(
|
||||
Entity $entity,
|
||||
string $relationName,
|
||||
string $id,
|
||||
array $columnData
|
||||
): void;
|
||||
|
||||
/**
|
||||
* Get a relationship column value.
|
||||
*
|
||||
* @return string|int|float|bool|null A relationship column value.
|
||||
*/
|
||||
public function getRelationColumn(
|
||||
Entity $entity,
|
||||
string $relationName,
|
||||
string $id,
|
||||
string $column
|
||||
): string|int|float|bool|null;
|
||||
|
||||
/**
|
||||
* Select related entities from DB.
|
||||
*
|
||||
* @return Collection<Entity>|Entity|null
|
||||
*/
|
||||
public function selectRelated(Entity $entity, string $relationName, ?Select $select = null): Collection|Entity|null;
|
||||
|
||||
/**
|
||||
* Get a number of related entities in DB.
|
||||
*/
|
||||
public function countRelated(Entity $entity, string $relationName, ?Select $select = null): int;
|
||||
}
|
||||
Reference in New Issue
Block a user