$db->beginTransaction(); $obj = new Lectures(array('lec_date' => $lec_date, 'attd_mst_id' => $queries['mst'])); $db->insert($obj); $db->prepare(); $db->execute(); $lec_id = $db->getLastGeneratedValue(); $db->setInsert(new Attendance()); $abs_objs = array(); foreach ($students['data'] as $stud) { //$ids[]=$stud['stud_id']; $presence = 1; if (in_array($stud['stud_id'], $abs_ids)) { $presence = 0; } $abs_objs[] = new Attendance(array('lec_id' => $lec_id, 'stud_id' => $stud['stud_id'], 'presence' => $presence)); } $db->multiInsert($abs_objs); $db->execute(); $db->commit(); echo json_encode(array('done' => true, 'final' => 'Attendance Added Successfully!')); } catch (\Exception $e) { $db->rollback(); $message = $e->getPrevious() ? $e->getPrevious()->getMessage() : $e->getMessage(); $code = $e->getPrevious() ? $e->getPrevious()->getCode() : $e->getCode(); $err = "Error Code: " . $code . " <br/>Detailed Info: " . $message; attd_error(HTTP_Status::INTERNAL_SERVER_ERROR, $err); } } else { attd_error($students['code'], isset($students['message']) ? $students['message'] : ""); } }