From 8dbcce3d388a219e0c99e642c662df74330816d7 Mon Sep 17 00:00:00 2001 From: TalysonSoares Date: Thu, 11 Apr 2024 21:23:42 -0300 Subject: [PATCH 1/3] =?UTF-8?q?adiciona=20fun=C3=A7=C3=A3o=20de=20excluir?= =?UTF-8?q?=20aluno?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/routes.php | 3 +++ src/Controller/AlunoController.php | 22 ++++++++++++++++++++-- views/alunos/listar.php | 3 ++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/config/routes.php b/config/routes.php index 86456c7..9584e6e 100644 --- a/config/routes.php +++ b/config/routes.php @@ -15,6 +15,9 @@ '/translate' => [TranslateController::class, 'translate'], + '/alunos/listar' => [AlunoController::class, 'listar'], + '/alunos/excluir' => [AlunoController::class, 'excluir'], + '/cursos/listar' => [CursoController::class, 'listar'], '/cursos/adicionar' => [CursoController::class, 'add'], '/cursos/editar' => [CursoController::class, 'editar'], diff --git a/src/Controller/AlunoController.php b/src/Controller/AlunoController.php index 4b11f1c..913ecbd 100644 --- a/src/Controller/AlunoController.php +++ b/src/Controller/AlunoController.php @@ -9,15 +9,33 @@ final class AlunoController extends AbstractController { + public mixed $entityManager; + + public function __construct() + { + $this->entityManager = parent::entityManager(); + } public function listar(): void { - $entityManager = parent::entityManager(); - $repository = $entityManager->getRepository(Aluno::class); + $repository = $this->entityManager->getRepository(Aluno::class); parent::render('alunos/listar', [ 'alunos' => $repository->findAll(), ]); } + + public function excluir(): void + { + $id = $_GET['id']; + $aluno = $this->entityManager->find(Aluno::class, $id); + + if($aluno !== null) { + $this->entityManager->remove($aluno); + $this->entityManager->flush(); + } + + header('location: /alunos/listar'); + } } \ No newline at end of file diff --git a/views/alunos/listar.php b/views/alunos/listar.php index 97ddbb1..37e7d88 100644 --- a/views/alunos/listar.php +++ b/views/alunos/listar.php @@ -24,11 +24,12 @@ {$aluno->created_at->format('d/m/Y')} {$buttonEdit} - {$buttonDelete} + {$buttonDelete} "; } ?> + From 490306b54a778b2bff5e9a6e4539fe330528fb9c Mon Sep 17 00:00:00 2001 From: ferreiranatan Date: Wed, 17 Apr 2024 19:15:53 -0300 Subject: [PATCH 2/3] first commit --- README.md | Bin 1256 -> 1344 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/README.md b/README.md index f329b644e36d5e633b9d951059eb26815f701147..852c20b1950238f4c403ba86b8d72e4f4c3c4378 100644 GIT binary patch delta 476 zcmXw#F-yZh6vr=37E>cmqLi4YwVNlXi-S5yc?ci4s|%;giXd0DbdA=c?)Qgr0bzoi!5|6# zS6@rcjn=FZgb`$y8AYs`_lB!;$QNV5RhJQlS1gA-1;L6kqm>`fi@qbrMq|Blm}qE- zX@-J9nE{P7%l32R1+IQRgD68L1THz!GaHt&b7b3e<)F50IP$6HRhI9y6;+MRdHGYT zdpg;Km_y7(5hO_krIdmW=$W9@ZS$-m%*j`?UQyo638l582%CqPRz7WE67Wq}Yk zm`~yD|t`o1iHye1x#? zBi25GAXw%VtaR=q7RwIo|9|`c^9J*!ei7_uK|(HgOOIiV_;e z(g28DfCoGKJe^JZ=cjzDVXU4C=`p2*6C*X)ZKt&f;1A<51_`MMZxXpUK3L_nI^YK( zW}{3H`2by%L(;5~W-L)tC^Zs>6x>IeL4O146iqaqWP*JtZhiad)aCde(R-7YAIGGRv zi+Tm2oKDR{!i;P7SM7&aVaD9c8Lc+>#Vd*luOZSXvP>HDNScG>oW+);3dwLe>`*Y) Tv5LT&(xrVb?J?}9#Ib$>(sX$S From abe599b540f866cfae6011767736963a533b42fa Mon Sep 17 00:00:00 2001 From: ferreiranatan Date: Wed, 17 Apr 2024 19:16:39 -0300 Subject: [PATCH 3/3] implementando o padrao no db --- bootstrap.php | 35 ++++++++++++++++-------- src/Adapter/DatabaseAdapterInterface.php | 8 ++++++ src/Adapter/MysqlAdapter.php | 27 ++++++++++++++++++ src/Adapter/PostgresAdapter.php | 31 +++++++++++++++++++++ 4 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/Adapter/DatabaseAdapterInterface.php create mode 100644 src/Adapter/MysqlAdapter.php create mode 100644 src/Adapter/PostgresAdapter.php diff --git a/bootstrap.php b/bootstrap.php index 20a719b..2d00e79 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -3,22 +3,35 @@ use Doctrine\DBAL\DriverManager; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; +use App\Adapter\MysqlAdapter; +use App\Adapter\PostgresAdapter; require_once "vendor/autoload.php"; -$config = ORMSetup::createAttributeMetadataConfiguration( - paths: [__DIR__."/src/Entity"], - isDevMode: true, -); - -$connection = DriverManager::getConnection([ - 'driver' => 'pdo_mysql', - 'dbname' => 'db_name', +$isDevMode = true; +$paths = [__DIR__."/src/Entity"]; +$dbParams = [ + 'driver' => 'pdo_mysql', 'user' => 'user', 'password' => 'password', 'host' => 'setup-mysql', -], $config); + 'dbname' => 'db_name', +]; + +// Configuração da conexão MySQL +$mysqlAdapter = new MysqlAdapter(); +$pdo_mysql = $mysqlAdapter->getConnection(); + +// Configuração da conexão PostgreSQL +$postgresAdapter = new PostgresAdapter(); +$pdo_postgres = $postgresAdapter->getConnection(); -$entityManager = new EntityManager($connection, $config); +// Crie a configuração do ORM +$ormConfig = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); +$entityManager = EntityManager::create($dbParams, $ormConfig); -return $entityManager; \ No newline at end of file +return [ + 'entityManager' => $entityManager, + 'pdo_mysql' => $pdo_mysql, + 'pdo_postgres' => $pdo_postgres +]; diff --git a/src/Adapter/DatabaseAdapterInterface.php b/src/Adapter/DatabaseAdapterInterface.php new file mode 100644 index 0000000..8626669 --- /dev/null +++ b/src/Adapter/DatabaseAdapterInterface.php @@ -0,0 +1,8 @@ + \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + ]; + + try { + $pdo = new \PDO($dsn, $user, $password, $options); + return $pdo; + } catch (\PDOException $e) { + throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } + } +} diff --git a/src/Adapter/PostgresAdapter.php b/src/Adapter/PostgresAdapter.php new file mode 100644 index 0000000..1b3ed4d --- /dev/null +++ b/src/Adapter/PostgresAdapter.php @@ -0,0 +1,31 @@ + \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + ]; + + try { + $pdo = new \PDO($dsn, $user, $password, $options); + return $pdo; + } catch (\PDOException $e) { + throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } +} +}