Today I came up with a legacy code. I have a function that sometimes generates very long runtimes and often occurs timeout at our partner. Mainly the function checks for reserved seats at a theatre but instead of get all the data and check for empty seats it runs the query for every single seat. which (i think) generates the huge runtime.
Here is the correspondig code:
public function printTicketChaos(){
$input = Input::all();
if( isset($input['date_id']) && !isset($input['reserve_id']) ){
$program_date = \Model\ProgramDate::find($input['date_id']);
if($input['piece'] > $program_date->available_capacity){
//dd($input['piece'] . ' ??? ' . $program_date->available_capacity);
return true;
}
$reserved_seats = Session::get('reserved_seats');
if($reserved_seats != null && count($reserved_seats) > 0){
foreach ($reserved_seats as $row => $seats) {
foreach ($seats as $key => $seat) {
$reserve = \Model\Reserve::whereForeignDateId($program_date->id)->whereTableName('programs')->whereHas('seats', function($query) use($row, $seat){ $query->whereRow($row)->whereSeat($seat); })->first();
if($reserve != null){
return true;
}
$oi = \Model\OrderItem::whereForeignId($program_date->id)->whereTableName('programs')->whereHas('order', function($query){ $query->whereStorno(0); })->whereHas('seats', function($query) use($row, $seat){ $query->whereRow($row)->whereSeat($seat); })->first();
if($oi != null){
return true;
}
}
}
}
}
return false;
}
I think the solution could be that i query all the seats and then check for the empty ones in the foreach. Is this a good idea or what do you think which is the best way to do it?
The code uses Laravel 4.2
Thank you for your aswers!