20241102#1
This commit is contained in:
37
application/base/Repository/AbstractMysqlRepository.php
Normal file
37
application/base/Repository/AbstractMysqlRepository.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Repository;
|
||||
|
||||
use Base\Domain\EntityInterface;
|
||||
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||
|
||||
abstract class AbstractMysqlRepository extends AbstractRepository {
|
||||
|
||||
public const DEBUG = false;
|
||||
|
||||
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;
|
||||
});
|
||||
if(static::DEBUG) dd($object, $in, $table);
|
||||
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());}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user