Implement delete
This commit is contained in:
@ -61,4 +61,8 @@ abstract class Model implements ModelInterface
|
|||||||
}
|
}
|
||||||
$this->isDirty();
|
$this->isDirty();
|
||||||
}
|
}
|
||||||
|
public function delete(): void
|
||||||
|
{
|
||||||
|
$this->getFactory()->get(get_class($this))->delete($this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,6 +100,12 @@ abstract class Repository implements RepositoryInterface
|
|||||||
}
|
}
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
public function delete(Model $model): void
|
||||||
|
{
|
||||||
|
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
|
||||||
|
$this->getConnection()->execute($query, [$model->getId()]);
|
||||||
|
}
|
||||||
|
|
||||||
public function fetchById(int $id): Model
|
public function fetchById(int $id): Model
|
||||||
{
|
{
|
||||||
$query = $this->getQueryBuilder()
|
$query = $this->getQueryBuilder()
|
||||||
|
@ -15,4 +15,5 @@ interface Model
|
|||||||
public function isDirty(): bool;
|
public function isDirty(): bool;
|
||||||
public function save(): void;
|
public function save(): void;
|
||||||
public function edit(array $data): void;
|
public function edit(array $data): void;
|
||||||
|
public function delete(): void;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,5 @@ interface Factory
|
|||||||
{
|
{
|
||||||
public function setContainer(ContainerInterface $container): Factory;
|
public function setContainer(ContainerInterface $container): Factory;
|
||||||
public function getContainer(): ContainerInterface;
|
public function getContainer(): ContainerInterface;
|
||||||
public function setNamespace(string $namespace): Factory;
|
public function get(string $repository_class): Repository;
|
||||||
public function getNamespace(): string;
|
|
||||||
public function get(string $repository_name): Repository;
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ interface Repository
|
|||||||
public function save(Model $model): void;
|
public function save(Model $model): void;
|
||||||
public function edit(Model $model, array $data): Model;
|
public function edit(Model $model, array $data): Model;
|
||||||
public function create(array $data): Model;
|
public function create(array $data): Model;
|
||||||
|
public function delete(Model $model): void;
|
||||||
public function fetchById(int $id): Model;
|
public function fetchById(int $id): Model;
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
||||||
}
|
}
|
||||||
|
@ -22,25 +22,8 @@ class Factory implements FactoryInterface
|
|||||||
{
|
{
|
||||||
return $this->container;
|
return $this->container;
|
||||||
}
|
}
|
||||||
protected string $namespace;
|
public function get(string $repository_class): Repository
|
||||||
public function setNamespace(string $namespace): FactoryInterface
|
|
||||||
{
|
{
|
||||||
$this->namespace = $namespace;
|
return $this->getContainer()->get($repository_class);
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function getNamespace(): string
|
|
||||||
{
|
|
||||||
return $this->namespace;
|
|
||||||
}
|
|
||||||
protected function buildRepository(string $repository_name): string
|
|
||||||
{
|
|
||||||
return implode("\\", [
|
|
||||||
$this->getNamespace(),
|
|
||||||
$repository_name
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
public function get(string $repository_name): Repository
|
|
||||||
{
|
|
||||||
return $this->getContainer()->get($this->buildRepository($repository_name));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user