This commit is contained in:
Yuri Kuznetsov
2023-02-17 18:16:57 +02:00
parent cc828661da
commit 1018bfd4d4
2 changed files with 574 additions and 528 deletions
+57 -528
View File
@@ -32,6 +32,7 @@ namespace Espo\ORM\Repository;
use Espo\ORM\EntityManager;
use Espo\ORM\EntityFactory;
use Espo\ORM\Collection;
use Espo\ORM\Repository\Deprecation\RDBRepositoryDeprecationTrait;
use Espo\ORM\Repository\Option\SaveOption;
use Espo\ORM\SthCollection;
use Espo\ORM\BaseEntity;
@@ -45,9 +46,7 @@ use Espo\ORM\Query\Part\Expression;
use Espo\ORM\Query\Part\Order;
use Espo\ORM\Mapper\BaseMapper;
use stdClass;
use RuntimeException;
use PDO;
/**
* A relation database repository.
@@ -57,6 +56,9 @@ use PDO;
*/
class RDBRepository implements Repository
{
/** @phpstan-use RDBRepositoryDeprecationTrait<TEntity> */
use RDBRepositoryDeprecationTrait;
protected HookMediator $hookMediator;
protected RDBTransactionManager $transactionManager;
@@ -114,22 +116,6 @@ class RDBRepository implements Repository
return $entity;
}
/**
* Get an entity. If ID is NULL, a new entity is returned.
*
* @return ?TEntity
*
* @deprecated Use `getById` and `getNew`.
*/
public function get(?string $id = null): ?Entity
{
if (is_null($id)) {
return $this->getNew();
}
return $this->getById($id);
}
protected function processCheckEntity(Entity $entity): void
{
if ($entity->getEntityType() !== $this->entityType) {
@@ -230,20 +216,6 @@ class RDBRepository implements Repository
$this->afterRemove($entity, $options);
}
/**
* @deprecated Use QueryBuilder instead.
*/
public function deleteFromDb(string $id, bool $onlyDeleted = false): void
{
$mapper = $this->getMapper();
if (!$mapper instanceof BaseMapper) {
throw new RuntimeException("Not supported 'deleteFromDb'.");
}
$mapper->deleteFromDb($this->entityType, $id, $onlyDeleted);
}
/**
* Find records.
*
@@ -258,7 +230,7 @@ class RDBRepository implements Repository
/**
* Find one record.
*
* @param ?array<string,mixed> $params @deprecated As of v6.0. Use query building.
* @param ?array<string, mixed> $params @deprecated As of v6.0. Use query building.
*/
public function findOne(?array $params = []): ?Entity
{
@@ -289,473 +261,7 @@ class RDBRepository implements Repository
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->find()`.
* @param ?array<string,mixed> $params
* @return Collection<TEntity>|TEntity|null
*/
public function findRelated(Entity $entity, string $relationName, ?array $params = null)
{
$params = $params ?? [];
if ($entity->getEntityType() !== $this->entityType) {
throw new RuntimeException("Not supported entity type.");
}
if (!$entity->hasId()) {
return null;
}
$type = $entity->getRelationType($relationName);
/** @phpstan-ignore-next-line */
$entityType = $entity->getRelationParam($relationName, 'entity');
$additionalColumns = $params['additionalColumns'] ?? [];
unset($params['additionalColumns']);
$additionalColumnsConditions = $params['additionalColumnsConditions'] ?? [];
unset($params['additionalColumnsConditions']);
$select = null;
if ($entityType) {
$params['from'] = $entityType;
$select = Select::fromRaw($params);
}
if ($type === Entity::MANY_MANY && count($additionalColumns)) {
if ($select === null) {
throw new RuntimeException();
}
$select = $this->applyRelationAdditionalColumns($entity, $relationName, $additionalColumns, $select);
}
// @todo Get rid of 'additionalColumnsConditions' usage. Use 'whereClause' instead.
if ($type === Entity::MANY_MANY && count($additionalColumnsConditions)) {
if ($select === null) {
throw new RuntimeException();
}
$select = $this->applyRelationAdditionalColumnsConditions(
$entity,
$relationName,
$additionalColumnsConditions,
$select
);
}
/** @var Collection<TEntity>|TEntity|null $result */
$result = $this->getMapper()->selectRelated($entity, $relationName, $select);
if ($result instanceof SthCollection) {
/** @var SthCollection<TEntity> */
return $this->entityManager->getCollectionFactory()->createFromSthCollection($result);
}
return $result;
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->count()`.
* @param ?array<string,mixed> $params
*/
public function countRelated(Entity $entity, string $relationName, ?array $params = null): int
{
$params = $params ?? [];
if ($entity->getEntityType() !== $this->entityType) {
throw new RuntimeException("Not supported entity type.");
}
if (!$entity->hasId()) {
return 0;
}
$type = $entity->getRelationType($relationName);
/** @phpstan-ignore-next-line */
$entityType = $entity->getRelationParam($relationName, 'entity');
$additionalColumnsConditions = $params['additionalColumnsConditions'] ?? [];
unset($params['additionalColumnsConditions']);
$select = null;
if ($entityType) {
$params['from'] = $entityType;
$select = Select::fromRaw($params);
}
if ($type === Entity::MANY_MANY && count($additionalColumnsConditions)) {
if ($select === null) {
throw new RuntimeException();
}
$select = $this->applyRelationAdditionalColumnsConditions(
$entity,
$relationName,
$additionalColumnsConditions,
$select
);
}
return (int) $this->getMapper()->countRelated($entity, $relationName, $select);
}
/**
* @param string[] $columns
*/
protected function applyRelationAdditionalColumns(
Entity $entity,
string $relationName,
array $columns,
Select $select
): Select {
if (empty($columns)) {
return $select;
}
/** @phpstan-ignore-next-line */
$middleName = lcfirst($entity->getRelationParam($relationName, 'relationName'));
$selectItemList = $select->getSelect();
if ($selectItemList === []) {
$selectItemList[] = '*';
}
foreach ($columns as $column => $alias) {
$selectItemList[] = [
$middleName . '.' . $column,
$alias
];
}
return $this->entityManager
->getQueryBuilder()
->select()
->clone($select)
->select($selectItemList)
->build();
}
/**
* @param array<string,mixed> $conditions
*/
protected function applyRelationAdditionalColumnsConditions(
Entity $entity,
string $relationName,
array $conditions,
Select $select
): Select {
if (empty($conditions)) {
return $select;
}
/** @phpstan-ignore-next-line */
$middleName = lcfirst($entity->getRelationParam($relationName, 'relationName'));
$builder = $this->entityManager
->getQueryBuilder()
->select()
->clone($select);
foreach ($conditions as $column => $value) {
$builder->where(
$middleName . '.' . $column,
$value
);
}
return $builder->build();
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->isRelated(...)`.
* @param TEntity|string $foreign
*/
public function isRelated(Entity $entity, string $relationName, $foreign): bool
{
if (!$entity->hasId()) {
return false;
}
if ($entity->getEntityType() !== $this->entityType) {
throw new RuntimeException("Not supported entity type.");
}
/** @var mixed $foreign */
if ($foreign instanceof Entity) {
if (!$foreign->hasId()) {
return false;
}
$id = $foreign->getId();
}
else if (is_string($foreign)) {
$id = $foreign;
}
else {
throw new RuntimeException("Bad 'foreign' value.");
}
if (!$id) {
return false;
}
if (in_array($entity->getRelationType($relationName), [Entity::BELONGS_TO, Entity::BELONGS_TO_PARENT])) {
if (!$entity->has($relationName . 'Id')) {
$entity = $this->getById($entity->getId());
}
}
/** @phpstan-var TEntity $entity */
$relation = $this->getRelation($entity, $relationName);
if ($foreign instanceof Entity) {
return $relation->isRelated($foreign);
}
return (bool) $this->countRelated($entity, $relationName, [
'whereClause' => [
'id' => $id,
],
]);
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->relate(...)`.
* @phpstan-ignore-next-line
*/
public function relate(Entity $entity, string $relationName, $foreign, $columnData = null, array $options = [])
{
if (!$entity->hasId()) {
throw new RuntimeException("Can't relate an entity w/o ID.");
}
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad 'foreign' value.");
}
if ($entity->getEntityType() !== $this->entityType) {
throw new RuntimeException("Not supported entity type.");
}
$this->beforeRelate($entity, $relationName, $foreign, $columnData, $options);
$beforeMethodName = 'beforeRelate' . ucfirst($relationName);
if (method_exists($this, $beforeMethodName)) {
$this->$beforeMethodName($entity, $foreign, $columnData, $options);
}
$result = false;
$methodName = 'relate' . ucfirst($relationName);
if (method_exists($this, $methodName)) {
$result = $this->$methodName($entity, $foreign, $columnData, $options);
}
else {
$data = $columnData;
if ($columnData instanceof stdClass) {
$data = get_object_vars($columnData);
}
if ($foreign instanceof Entity) {
$result = $this->getMapper()->relate($entity, $relationName, $foreign, $data);
}
else {
$id = $foreign;
$result = $this->getMapper()->relateById($entity, $relationName, $id, $data);
}
}
if ($result) {
$this->afterRelate($entity, $relationName, $foreign, $columnData, $options);
$afterMethodName = 'afterRelate' . ucfirst($relationName);
if (method_exists($this, $afterMethodName)) {
$this->$afterMethodName($entity, $foreign, $columnData, $options);
}
}
return $result;
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->unrelate(...)`.
* @phpstan-ignore-next-line
*/
public function unrelate(Entity $entity, string $relationName, $foreign, array $options = [])
{
if (!$entity->hasId()) {
throw new RuntimeException("Can't unrelate an entity w/o ID.");
}
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad foreign value.");
}
if ($entity->getEntityType() !== $this->entityType) {
throw new RuntimeException("Not supported entity type.");
}
$this->beforeUnrelate($entity, $relationName, $foreign, $options);
$beforeMethodName = 'beforeUnrelate' . ucfirst($relationName);
if (method_exists($this, $beforeMethodName)) {
$this->$beforeMethodName($entity, $foreign, $options);
}
$result = false;
$methodName = 'unrelate' . ucfirst($relationName);
if (method_exists($this, $methodName)) {
$this->$methodName($entity, $foreign);
}
else {
if ($foreign instanceof Entity) {
$this->getMapper()->unrelate($entity, $relationName, $foreign);
}
else {
$id = $foreign;
$this->getMapper()->unrelateById($entity, $relationName, $id);
}
}
$this->afterUnrelate($entity, $relationName, $foreign, $options);
$afterMethodName = 'afterUnrelate' . ucfirst($relationName);
if (method_exists($this, $afterMethodName)) {
$this->$afterMethodName($entity, $foreign, $options);
}
return $result;
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->getColumn(...)`.
* @phpstan-ignore-next-line
*/
public function getRelationColumn(Entity $entity, string $relationName, string $foreignId, string $column)
{
return $this->getMapper()->getRelationColumn($entity, $relationName, $foreignId, $column);
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function beforeRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = [])
{
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function afterRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = [])
{
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function beforeUnrelate(Entity $entity, $relationName, $foreign, array $options = [])
{
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function afterUnrelate(Entity $entity, $relationName, $foreign, array $options = [])
{
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function beforeMassRelate(Entity $entity, $relationName, array $params = [], array $options = [])
{
}
/**
* @deprecated As of v6.0.
* @phpstan-ignore-next-line
*/
protected function afterMassRelate(Entity $entity, $relationName, array $params = [], array $options = [])
{
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->updateColumns(...)`.
* @phpstan-ignore-next-line
*/
public function updateRelation(Entity $entity, string $relationName, $foreign, $columnData)
{
if (!$entity->hasId()) {
throw new RuntimeException("Can't update a relation for an entity w/o ID.");
}
if (!$foreign instanceof Entity && !is_string($foreign)) {
throw new RuntimeException("Bad foreign value.");
}
if ($columnData instanceof stdClass) {
$columnData = get_object_vars($columnData);
}
if ($foreign instanceof Entity) {
$id = $foreign->getId();
} else {
$id = $foreign;
}
if (!is_string($id)) {
throw new RuntimeException("Bad foreign value.");
}
$this->getMapper()->updateRelationColumns($entity, $relationName, $id, $columnData);
return true;
}
/**
* @deprecated As of v6.0. Use `getRelation(...)->massRelate(...)`.
* @phpstan-ignore-next-line
*/
public function massRelate(Entity $entity, string $relationName, array $params = [], array $options = [])
{
if (!$entity->hasId()) {
throw new RuntimeException("Can't related an entity w/o ID.");
}
$this->beforeMassRelate($entity, $relationName, $params, $options);
$select = Select::fromRaw($params);
$this->getMapper()->massRelate($entity, $relationName, $select);
$this->afterMassRelate($entity, $relationName, $params, $options);
}
/**
* @param array<string,mixed> $params @deprecated Use query building.
* @param array<string, mixed> $params @deprecated Use query building.
*/
public function count(array $params = []): int
{
@@ -968,24 +474,6 @@ class RDBRepository implements Repository
return $this->createSelectBuilder()->group($groupBy);
}
/**
* @deprecated Use `group` method.
* @param Expression|Expression[]|string|string[] $groupBy
* @return RDBSelectBuilder<TEntity>
*/
public function groupBy($groupBy): RDBSelectBuilder
{
return $this->group($groupBy);
}
/**
* @deprecated As of v7.0. Use the Query Builder instead. Otherwise, code will be not portable.
*/
protected function getPDO(): PDO
{
return $this->entityManager->getPDO();
}
/**
* Create a select builder.
*
@@ -1000,7 +488,8 @@ class RDBRepository implements Repository
}
/**
* @param array<string,mixed> $options
* Use hooks instead.
* @param array<string, mixed> $options
* @return void
*/
protected function beforeSave(Entity $entity, array $options = [])
@@ -1009,7 +498,8 @@ class RDBRepository implements Repository
}
/**
* @param array<string,mixed> $options
* Use hooks instead.
* @param array<string, mixed> $options
* @return void
*/
protected function afterSave(Entity $entity, array $options = [])
@@ -1018,7 +508,8 @@ class RDBRepository implements Repository
}
/**
* @param array<string,mixed> $options
* Use hooks instead.
* @param array<string, mixed> $options
* @return void
*/
protected function beforeRemove(Entity $entity, array $options = [])
@@ -1027,7 +518,8 @@ class RDBRepository implements Repository
}
/**
* @param array<string,mixed> $options
* Use hooks instead.
* @param array<string, mixed> $options
* @return void
*/
protected function afterRemove(Entity $entity, array $options = [])
@@ -1037,17 +529,54 @@ class RDBRepository implements Repository
protected function getMapper(): RDBMapper
{
/** @var RDBMapper $mapper */
$mapper = $this->entityManager->getMapper();
if (!$mapper instanceof RDBMapper) {
throw new RuntimeException("Mapper is not RDB.");
}
return $mapper;
}
/**
* @deprecated Use `$this->entityManager`.
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function getEntityManager(): EntityManager
{
return $this->entityManager;
}
protected function beforeRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = [])
{}
/**
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function afterRelate(Entity $entity, $relationName, $foreign, $data = null, array $options = [])
{}
/**
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function beforeUnrelate(Entity $entity, $relationName, $foreign, array $options = [])
{}
/**
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function afterUnrelate(Entity $entity, $relationName, $foreign, array $options = [])
{}
/**
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function beforeMassRelate(Entity $entity, $relationName, array $params = [], array $options = [])
{}
/**
* @deprecated As of v6.0. Use hooks instead.
* @phpstan-ignore-next-line
*/
protected function afterMassRelate(Entity $entity, $relationName, array $params = [], array $options = [])
{}
}