Optimizacion de queries a cargar de una sola vez
This commit is contained in:
@ -296,16 +296,65 @@ class Venta extends Ideal\Repository
|
||||
* @return Model\Venta
|
||||
* @throws Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByUnidadId(int $unidad_id): Model\Venta
|
||||
public function fetchActiveByUnidadId(int $unidad_id): Model\Venta
|
||||
{
|
||||
$subSubQuery = $this->connection->getQueryBuilder()
|
||||
->select('MAX(`id`) AS `id`, `venta`')
|
||||
->from('`estado_venta`')
|
||||
->group('`venta`');
|
||||
$subQuery = $this->connection->getQueryBuilder()
|
||||
->select('e1.*')
|
||||
->from('`estado_venta` e1')
|
||||
->joined("INNER JOIN ({$subSubQuery}) e0 ON e0.`id` = e1.`id`");
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad')
|
||||
->where('pu.unidad = ?');
|
||||
->joined('INNER JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad')
|
||||
->joined("INNER JOIN ({$subQuery}) ev ON ev.`venta` = a.`id`")
|
||||
->joined('INNER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`')
|
||||
->where('pu.unidad = ? AND tev.activa = 1');
|
||||
return $this->fetchOne($query, [$unidad_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $unidad_ids
|
||||
* @return array
|
||||
* @throws Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchActiveByUnidadIds(array $unidad_ids): array
|
||||
{
|
||||
$subSubQuery = $this->connection->getQueryBuilder()
|
||||
->select('MAX(`id`) AS `id`, `venta`')
|
||||
->from('`estado_venta`')
|
||||
->group('`venta`');
|
||||
$subQuery = $this->connection->getQueryBuilder()
|
||||
->select('e1.*')
|
||||
->from('`estado_venta` e1')
|
||||
->joined("INNER JOIN ({$subSubQuery}) e0 ON e0.`id` = e1.`id`");
|
||||
$interrogations = implode(',', array_fill(0, count($unidad_ids), '?'));
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*, pu.unidad')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('INNER JOIN propiedad_unidad pu ON pu.propiedad = a.propiedad')
|
||||
->joined("INNER JOIN ({$subQuery}) ev ON ev.`venta` = a.`id`")
|
||||
->joined('INNER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado`')
|
||||
->where("pu.unidad IN ({$interrogations}) AND tev.activa = 1");
|
||||
try {
|
||||
$results = $this->connection->execute($query, $unidad_ids)->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (empty($results)) {
|
||||
throw new Implement\Exception\EmptyResult($query);
|
||||
}
|
||||
return array_map(function($result) {
|
||||
return [
|
||||
'unidad_id' => $result['unidad'],
|
||||
'venta' => $this->load($result)
|
||||
];
|
||||
}, $results);
|
||||
} catch (PDOException $exception) {
|
||||
throw new Implement\Exception\EmptyResult($query, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unidad
|
||||
* @param string $tipo
|
||||
|
Reference in New Issue
Block a user