20240502#1

This commit is contained in:
User
2024-05-02 11:29:58 +03:00
parent 88b597d78d
commit e03c9bccfc
31 changed files with 208 additions and 147 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace Base\Infrastructure\Repository;
use Base\Domain\EntityInterface;
use Rmphp\Storage\Mysql\MysqlStorageInterface;
abstract class AbstractMysqlRepository extends AbstractRepository {
public function __construct(
public readonly MysqlStorageInterface $mysql
) {}
/**
* @param EntityInterface $object
* @param string $table
* @return mixed
* @throws RepositoryException
*/
public function saveEntity(EntityInterface $object, string $table) : mixed {
$in = $this->getProperties($object, function ($value){
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
});
//dd($object, $in);
try {
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
$this->mysql->updateById($table, $in, $object->getId());
return $object->getId();
} else {
$this->mysql->insert($table, $in);
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
}
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
}
}