52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository;
|
||
|
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class Provincia extends Ideal\Repository
|
||
|
{
|
||
|
public function __construct(Define\Connection $connection, protected Region $regionRepository)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('provincia');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Define\Model
|
||
|
{
|
||
|
$map = [
|
||
|
'descripcion' => [],
|
||
|
'region' => [
|
||
|
'function' => function($data) {
|
||
|
return $this->regionRepository->fetchById($data['region']);
|
||
|
}
|
||
|
]
|
||
|
];
|
||
|
return $this->parseData(new Model\Provincia(), $data, $map);
|
||
|
}
|
||
|
public function save(Define\Model $model): Define\Model
|
||
|
{
|
||
|
$model->id = $this->saveNew(
|
||
|
['descripcion', 'region'],
|
||
|
[$model->descripcion, $model->region->id]
|
||
|
);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
||
|
{
|
||
|
return $this->update($model, ['descripcion', 'region'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByDescripcion(string $description): Define\Model
|
||
|
{
|
||
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `descripcion` = ?";
|
||
|
return $this->fetchOne($query, [$description]);
|
||
|
}
|
||
|
public function fetchByRegion(int $region_id): array
|
||
|
{
|
||
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `region` = ?";
|
||
|
return $this->fetchMany($query, [$region_id]);
|
||
|
}
|
||
|
}
|