20250126#2

This commit is contained in:
User
2025-01-26 14:56:59 +03:00
parent ac572cc92b
commit 2cfc92584c
2 changed files with 14 additions and 27 deletions

View File

@@ -51,21 +51,18 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
/** @inheritDoc */ /** @inheritDoc */
public function saveEntity(EntityInterface $object, string $table = null) : mixed { public function saveEntity(EntityInterface $object) : mixed {
if(!isset($table)){ $this->checkConst();
$this->checkConst();
$table = static::TABLE;
}
$in = $this->getProperties($object, function ($value){ $in = $this->getProperties($object, function ($value){
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value; return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
}); });
if(static::DEBUG) {$this->getSaveDebug($object, $in, $table); exit;} if(static::DEBUG) {$this->getSaveDebug($object, $in, static::TABLE); exit;}
try { try {
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) { if (!empty($object->getId()) && !empty($this->mysql->findById(static::TABLE, $object->getId()))) {
$this->mysql->updateById($table, $in, $object->getId()); $this->mysql->updateById(static::TABLE, $in, $object->getId());
return $object->getId(); return $object->getId();
} else { } else {
$this->mysql->insert($table, $in); $this->mysql->insert(static::TABLE, $in);
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id; return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
} }
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());} } catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
@@ -73,14 +70,10 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
/** @inheritDoc */ /** @inheritDoc */
public function saveGroup(array $objects, string $table = null): array { public function saveGroup(array $objects): array {
if(!isset($table)){
$this->checkConst();
$table = static::TABLE;
}
try{ try{
$this->mysql->mysql()->begin_transaction(); $this->mysql->mysql()->begin_transaction();
foreach($objects as $object) $id[] = $this->saveEntity($object, $table); foreach($objects as $object) $id[] = $this->saveEntity($object);
$this->mysql->mysql()->commit(); $this->mysql->mysql()->commit();
return $id ?? []; return $id ?? [];
} }
@@ -92,13 +85,10 @@ abstract class AbstractMysqlRepository extends AbstractRepository implements Mys
/** @inheritDoc */ /** @inheritDoc */
public function deleteEntity(EntityInterface $object, string $table = null) : bool { public function deleteEntity(EntityInterface $object) : bool {
if(!isset($table)){ $this->checkConst();
$this->checkConst();
$table = static::TABLE;
}
if(!empty($object->getId())){ if(!empty($object->getId())){
return $this->mysql->deleteById($table, $object->getId()); return $this->mysql->deleteById(static::TABLE, $object->getId());
} }
return false; return false;
} }

View File

@@ -39,27 +39,24 @@ interface MysqlRepositoryInterface extends RepositoryInterface {
/** /**
* @param EntityInterface $object * @param EntityInterface $object
* @param string|null $table
* @return mixed * @return mixed
* @throws RepositoryException * @throws RepositoryException
*/ */
public function saveEntity(EntityInterface $object, string $table = null) : mixed; public function saveEntity(EntityInterface $object) : mixed;
/** /**
* @param array $objects * @param array $objects
* @param string|null $table
* @return array * @return array
* @throws RepositoryException * @throws RepositoryException
*/ */
public function saveGroup(array $objects, string $table = null): array; public function saveGroup(array $objects): array;
/** /**
* @param EntityInterface $object * @param EntityInterface $object
* @param string|null $table
* @return bool * @return bool
* @throws RepositoryException * @throws RepositoryException
*/ */
public function deleteEntity(EntityInterface $object, string $table = null) : bool; public function deleteEntity(EntityInterface $object) : bool;
/** /**
* @return array * @return array