49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository;
|
||
|
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Common\Ideal\Repository;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class Direccion extends Repository
|
||
|
{
|
||
|
public function __construct(Define\Connection $connection, protected Comuna $comunaRepository)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('direccion');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Define\Model
|
||
|
{
|
||
|
$map = [
|
||
|
'calle' => [],
|
||
|
'numero' => [],
|
||
|
'extra' => [],
|
||
|
'comuna' => [
|
||
|
'function' => function($data) {
|
||
|
return $this->comunaRepository->fetchById($data['comuna']);
|
||
|
}
|
||
|
]
|
||
|
];
|
||
|
return $this->parseData(new Model\Direccion(), $data, $map);
|
||
|
}
|
||
|
public function save(Define\Model $model): Define\Model
|
||
|
{
|
||
|
$model->id = $this->saveNew(
|
||
|
['calle', 'numero', 'extra', 'comuna'],
|
||
|
[$model->calle, $model->numero, $model->extra, $model->comuna->id]
|
||
|
);
|
||
|
return $model;
|
||
|
}
|
||
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
||
|
{
|
||
|
return $this->update($model, ['calle', 'numero', 'extra', 'comuna'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByCalleAndNumero(string $calle, int $numero): array
|
||
|
{
|
||
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `calle` = ? AND `numero` = ?";
|
||
|
return $this->fetchMany($query, [$calle, $numero]);
|
||
|
}
|
||
|
}
|