function bindAppointmentEntityArray($appointmentEntitys)
{
    $appointmentDtos = new AppointmentListDto();
    $appointmentDtoArray = array();
    foreach ($appointmentEntitys as $appointmentEntity => $value) {
        array_push($appointmentDtoArray, bindAppointmentEntity($value));
    }
    $appointmentDtos->setAppointments($appointmentDtoArray);
    return $appointmentDtos;
}
function bindPrescriptionEntity($prescriptionEntity)
{
    if ($prescriptionEntity != null) {
        $prescriptionDto = new PrescriptionDto();
        $prescriptionDto->setPrescriptionId($prescriptionEntity->getPrescriptionId());
        $prescriptionDto->setAppointment(bindAppointmentEntity($prescriptionEntity->getAppointment()));
        $prescriptionDto->setDoctor(bindDoctorEntity($prescriptionEntity->getDoctor()));
        $prescriptionDto->setUser(bindUserEntity($prescriptionEntity->getUser()));
        $prescriptionDto->setFile(bindFileEntity($prescriptionEntity->getFile()));
        $prescriptionDto->setEffFrm($prescriptionEntity->getEffFrm());
        $prescriptionDto->setEffTo($prescriptionEntity->getEffTo());
        return $prescriptionDto;
    } else {
        return null;
    }
}
    $appointmentsArray = array();
    foreach ($appointmentListDto->getAppointments() as $appointmentDto) {
        $appointmentEntity = bindAppointmentDto($appointmentDto);
        $entityManager->persist($appointmentEntity);
        $entityManager->flush();
        array_push($appointmentsArray, bindAppointmentEntity($appointmentEntity));
    }
    $appointmentListDto = new AppointmentListDto();
    $appointmentListDto->setAppointments($appointmentsArray);
    $appointmentListDto->printData($app);
});
$app->put('/appointments/:id', function ($id) use($app) {
    global $entityManager;
    $appointmentEntity = $entityManager->find("AppointmentEntity", $id);
    $entityManager->flush();
    $appointmentDto = bindAppointmentEntity($appointmentEntity);
    $appointmentDto->printData($app);
});
$app->delete('/appointments/:id', function ($id) use($app) {
    global $entityManager;
    $appointmentEntity = $entityManager->find("AppointmentEntity", $id);
    $entityManager->remove($appointmentEntity);
    $entityManager->flush();
});
/*Referances*/
$app->get('/appointments/:id/prescriptions/appointments', function ($id) use($app) {
    global $entityManager;
    $prescriptionEntities = $entityManager->getRepository("PrescriptionEntity")->findBy(array('appointment' => $id));
    $prescription = bindPrescriptionEntityArray($prescriptionEntities);
    $prescription->printData($app);
});