Exemplo n.º 1
0
 public static function delete($id)
 {
     $connection = Flight::dbMain();
     try {
         /* Begin Transaction */
         $connection->beginTransaction();
         /*Query 1 Select unit*/
         $sql = "SELECT * FROM unit WHERE id = :id;";
         $query = $connection->prepare($sql);
         $query->bindParam(':id', $id, PDO::PARAM_INT);
         $query->execute();
         $rows = $query->fetchAll(PDO::FETCH_ASSOC);
         $row = $rows[0];
         $unit = new Unit();
         $unit->Id = (int) $row['id'];
         $unit->Imei = $row['unit_imei'];
         $unit->DtCreated = $row['unit_dt_created'];
         $unit->SerialNumber = $row['unit_serial_number'];
         $unit->Sim = Sim::select($row['sim_id']);
         $unit->UnitStatus = UnitStatus::select($row['e_status_unit_id']);
         $unit->UnitType = UnitType::select($row['unit_type_id']);
         $unit->Company = Company::select($row['company_id']);
         /*Query 2 Delete unit*/
         $sql = "\n\t\t\tDELETE FROM unit \n\t\t\tWHERE\n\t\t\tid = :id";
         $query = $connection->prepare($sql);
         $query->bindParam(':id', $id, PDO::PARAM_INT);
         $query->execute();
         /*Query 3 Drop data_unit.imei table*/
         $year = date('Y');
         $schema = "app_data_{$year}";
         $imei = $unit->Imei;
         $tableName = "data_{$imei}";
         $sql = "\n\t\t\t\n\t\t\tDROP TABLE IF EXISTS {$schema}.{$tableName};\n\n\t\t\t";
         $query = $connection->prepare($sql);
         $query->execute();
         $connection->commit();
         $result = new Result();
         $result->Status = Result::DELETED;
         $result->Message = 'Done';
         $result->Id = $id;
         return $result;
     } catch (PDOException $pdoException) {
         $connection->rollBack();
         throw $pdoException;
     } catch (Exception $exception) {
         $connection->rollBack();
         throw $exception;
     } finally {
         $connection = null;
     }
 }
Exemplo n.º 2
0
    }
});
//=============================================================================
//UnitStatus
//=============================================================================
Flight::route('GET /v1/main/unitstatus', function () {
    try {
        $array = UnitStatus::selectAll();
        Flight::ok($array);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
Flight::route('GET /v1/main/unitstatus/@id', function ($id) {
    try {
        $object = UnitStatus::select($id);
        Flight::ok($object);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
Flight::route('POST /v1/main/unitstatus', function () {
    try {
        $object = UnitStatus::insert();
        Flight::ok($object);
    } catch (Exception $exception) {
        Flight::error($exception);
    }
});
Flight::route('PUT /v1/main/unitstatus/@id', function ($id) {
    try {