Orden de queries
This commit is contained in:
@ -51,15 +51,16 @@ class Precio extends Ideal\Repository
|
|||||||
*/
|
*/
|
||||||
public function fetchByProyecto(int $proyecto_id): array
|
public function fetchByProyecto(int $proyecto_id): array
|
||||||
{
|
{
|
||||||
$query = "SELECT a.*
|
$query = $this->connection->getQueryBuilder()
|
||||||
FROM `{$this->getTable()}` a
|
->select('a.*')
|
||||||
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
|
->from("{$this->getTable()} a")
|
||||||
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
|
->joined($this->joinEstado())
|
||||||
JOIN `unidad` ON `unidad`.`id` = a.`unidad`
|
->joined('INNER JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`')
|
||||||
JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`
|
->joined('INNER JOIN `unidad` ON `unidad`.`id` = a.`unidad`')
|
||||||
JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`
|
->joined('INNER JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt`')
|
||||||
WHERE ptu.`proyecto` = ? AND tep.`descripcion` = 'vigente'
|
->joined('INNER JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo`')
|
||||||
ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, '0')";
|
->where('ptu.`proyecto` = ? AND tep.`descripcion` = "vigente"')
|
||||||
|
->order('tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, "0")');
|
||||||
return $this->fetchMany($query, [$proyecto_id]);
|
return $this->fetchMany($query, [$proyecto_id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,11 +71,12 @@ ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcio
|
|||||||
*/
|
*/
|
||||||
public function fetchByUnidad(int $unidad_id): array
|
public function fetchByUnidad(int $unidad_id): array
|
||||||
{
|
{
|
||||||
$query = "SELECT a.*
|
$query = $this->connection->getQueryBuilder()
|
||||||
FROM `{$this->getTable()}` a
|
->select('a.*')
|
||||||
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
|
->from("{$this->getTable()} a")
|
||||||
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
|
->joined($this->joinEstado())
|
||||||
WHERE `unidad` = ?";
|
->joined('INNER JOIN tipo_estado_precio tep ON tep.`id` = ep.`estado`')
|
||||||
|
->where('`unidad` = ?');
|
||||||
return $this->fetchMany($query, [$unidad_id]);
|
return $this->fetchMany($query, [$unidad_id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,11 +87,12 @@ WHERE `unidad` = ?";
|
|||||||
*/
|
*/
|
||||||
public function fetchVigenteByUnidad(int $unidad_id): Define\Model
|
public function fetchVigenteByUnidad(int $unidad_id): Define\Model
|
||||||
{
|
{
|
||||||
$query = "SELECT a.*
|
$query = $this->connection->getQueryBuilder()
|
||||||
FROM `{$this->getTable()}` a
|
->select('a.*')
|
||||||
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
|
->from("{$this->getTable()} a")
|
||||||
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
|
->joined($this->joinEstado())
|
||||||
WHERE `unidad` = ? AND tep.`descripcion` = 'vigente'";
|
->joined('INNER JOIN tipo_estado_precio tep ON tep.`id` = ep.`estado`')
|
||||||
|
->where('`unidad` = ? AND tep.`descripcion` = "vigente"');
|
||||||
return $this->fetchOne($query, [$unidad_id]);
|
return $this->fetchOne($query, [$unidad_id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,11 +104,25 @@ WHERE `unidad` = ? AND tep.`descripcion` = 'vigente'";
|
|||||||
*/
|
*/
|
||||||
public function fetchByUnidadAndDate(int $unidad_id, string $date_time): Define\Model
|
public function fetchByUnidadAndDate(int $unidad_id, string $date_time): Define\Model
|
||||||
{
|
{
|
||||||
$query = "SELECT a.*
|
$query = $this->connection->getQueryBuilder()
|
||||||
FROM `{$this->getTable()}` a
|
->select('a.*')
|
||||||
JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id`
|
->from("{$this->getTable()} a")
|
||||||
JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`
|
->joined($this->joinEstado())
|
||||||
WHERE `unidad` = ? AND ep.`fecha` <= ? AND tep.`descripcion` = 'vigente'";
|
->joined('INNER JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado`')
|
||||||
|
->where('`unidad` = ? AND ep.`fecha` <= ? AND tep.`descripcion` = "vigente"');
|
||||||
return $this->fetchOne($query, [$unidad_id, $date_time]);
|
return $this->fetchOne($query, [$unidad_id, $date_time]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function joinEstado(): string
|
||||||
|
{
|
||||||
|
$subSubQuery = $this->connection->getQueryBuilder()
|
||||||
|
->select('MAX(id) AS id, precio')
|
||||||
|
->from('estado_precio')
|
||||||
|
->group('precio');
|
||||||
|
$subQuery = $this->connection->getQueryBuilder()
|
||||||
|
->select('e1.*')
|
||||||
|
->from('estado_precio e1')
|
||||||
|
->joined("INNER JOIN ($subSubQuery) e0 ON e0.id = e1.id");
|
||||||
|
return "JOIN ($subQuery) ep ON ep.precio = a.id";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user