setTable('propiedad_unidad'); } public function load(array $data_row): Define\Model { $unidad = $this->unidadRepository->fetchById($data_row['unidad']); $data = [ 'id' => $unidad->id, 'subtipo' => $unidad->subtipo, 'piso' => $unidad->piso, 'descripcion' => $unidad->descripcion, 'orientacion' => $unidad->orientacion, 'prorrateo' => $unidad->prorrateo, 'pt' => $unidad->proyectoTipoUnidad->id, 'pu_id' => $data_row['id'], 'propiedad' => $data_row['propiedad'], 'valor' => $data_row['valor'] ]; return parent::load($data); } public function create(?array $data = null): Model\Venta\PropiedadUnidad { $map = (new Implement\Repository\MapperParser(['subtipo', 'piso', 'descripcion', 'orientacion', 'prorrateo', 'valor', 'id'])) ->register('propiedad', (new Implement\Repository\Mapper()) ->setProperty('propiedad_id')) ->register('pt', (new Implement\Repository\Mapper()) ->setProperty('proyectoTipoUnidad') ->setFunction(function($data) { return $this->proyectoTipoUnidadService->getById($data['pt']); })); return $this->parseData(new Model\Venta\PropiedadUnidad(), $data, $map); } public function save(Define\Model $model): Model\Venta\PropiedadUnidad { $model->pu_id = $this->saveNew(['propiedad', 'unidad', 'valor'], [$model->propiedad_id, $model->id, $model->valor]); return $model; } public function edit(Define\Model $model, array $new_data): Model\Venta\PropiedadUnidad { return $this->update($model, ['propiedad', 'unidad', 'valor'], $new_data); } public function fetchById(int $id): Define\Model { $query = $this->connection->getQueryBuilder() ->select() ->from($this->getTable()) ->where("id = ?"); return $this->fetchOne($query, [$id]); } public function fetchByVenta(int $venta_id): array { $query = $this->connection->getQueryBuilder() ->select('a.*') ->from("{$this->getTable()} a") ->joined('JOIN unidad ON a.unidad = unidad.id JOIN venta ON venta.propiedad = a.propiedad') ->where('venta.id = ?'); return $this->fetchMany($query, [$venta_id]); } public function fetchByPropiedad(int $propiedad_id): array { $query = $this->connection->getQueryBuilder() ->select('a.*') ->from("{$this->getTable()} a") ->joined('JOIN `unidad` ON a.`unidad` = `unidad`.`id`') ->where('a.`propiedad` = ?') ->group('`unidad`.`id`'); return $this->fetchMany($query, [$propiedad_id]); } protected function update(Define\Model $model, array $columns, array $data): Define\Model { $changes = []; $values = []; foreach ($columns as $column) { if (isset($data[$column])) { $changes []= $column; $values []= $data[$column]; } } if (count($changes) === 0) { return $model; } $columns_string = implode(', ', array_map(function($property) {return "`{$property}` = ?";}, $changes)); $query = $this->connection->getQueryBuilder() ->update($this->getTable()) ->set($columns_string) ->where("id = ?"); $values []= $model->{$this->getKey()}; $this->connection->execute($query, $values); return $this->fetchById($model->{$this->getKey()}); } protected function getKey(): string { return 'pu_id'; } }