Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified README.md
Binary file not shown.
35 changes: 24 additions & 11 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
return [
'entityManager' => $entityManager,
'pdo_mysql' => $pdo_mysql,
'pdo_postgres' => $pdo_postgres
];
3 changes: 3 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
8 changes: 8 additions & 0 deletions src/Adapter/DatabaseAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace App\Adapter;

interface DatabaseAdapterInterface {
public function getConnection(): \PDO;
}
27 changes: 27 additions & 0 deletions src/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Adapter;

use App\Adapter\DatabaseAdapterInterface;

class MysqlAdapter implements DatabaseAdapterInterface {
public function getConnection(): \PDO {
$host = 'setup-mysql';
$dbname = 'db_name';
$user = 'user';
$password = 'password';

$dsn = "mysql:host=$host;dbname=$dbname";
$options = [
\PDO::ATTR_ERRMODE => \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());
}
}
}
31 changes: 31 additions & 0 deletions src/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace App\Adapter;

use App\Adapter\DatabaseAdapterInterface;


class PostgresAdapter implements DatabaseAdapterInterface
{

public function getConnection(): \PDO {
$host = 'setup-postgres';
$dbname = 'db_name';
$user = 'user';
$password = 'password';

$dsn = "pgsql:host=$host;dbname=$dbname";
$options = [
\PDO::ATTR_ERRMODE => \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());
}
}
}
22 changes: 20 additions & 2 deletions src/Controller/AlunoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
3 changes: 2 additions & 1 deletion views/alunos/listar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
<td>{$aluno->created_at->format('d/m/Y')}</td>
<td>
<a href='' class='btn btn-warning btn-sm'>{$buttonEdit}</a>
<a href='' class='btn btn-danger btn-sm'>{$buttonDelete}</a>
<a href='/alunos/excluir?id={$aluno->id}' class='btn btn-danger btn-sm'>{$buttonDelete}</a>
</td>
</tr>
";
}
?>

</tbody>
</table>