Skip to content

Commit

Permalink
App: connect to postgres and mariadb
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 6, 2024
1 parent af70ab8 commit 7e59609
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ https://examples.contributte.org/doctrine-skeleton/

## Installation

You will need `PHP 8.1+` and [Composer](https://getcomposer.org/).
You will need `PHP 8.3+` and [Composer](https://getcomposer.org/).

Create project using composer.

Expand Down
18 changes: 12 additions & 6 deletions app/UI/Home/HomePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,40 @@
namespace App\UI\Home;

use App\Domain\Database\User;
use App\Model\Database\EntityManagerDecorator;
use App\UI\BasePresenter;
use Nette\DI\Attributes\Inject;
use Nette\Utils\Random;
use Nettrine\ORM\ManagerRegistry;

class HomePresenter extends BasePresenter
{

#[Inject]
public EntityManagerDecorator $em;
public ManagerRegistry $managerRegistry;

public function actionDefault(): void
{
$this->template->users = $this->em->getRepository(User::class)->findAll();
// PostgreSQL
$users = $this->managerRegistry->getManager('default')->getRepository(User::class)->findAll();

// MariaDB
$users2 = $this->managerRegistry->getManager('second')->getRepository(User::class)->findAll();

$this->template->users = [...$users, ...$users2];
}

public function handleCreateUser(): void
{
$user = new User(Random::generate(20));
$this->em->persist($user);
$this->em->flush();
$this->managerRegistry->getManager('default')->persist($user);
$this->managerRegistry->getManager('default')->flush();
$this->flashMessage('Saved');
$this->redirect('this');
}

public function handleDeleteUsers(): void
{
$this->em->getRepository(User::class)
$this->managerRegistry->getManager('default')->getRepository(User::class)
->createQueryBuilder('u')
->delete()
->getQuery()
Expand Down
21 changes: 14 additions & 7 deletions config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ php:
# ======================================
# Parameters ===========================
parameters:
database:
driver: pdo_sqlite
host: null
dbname: null
port: null
user: null
password: null
postgres:
driver: pdo_pgsql
host: localhost
port: 5432
user: contributte
password: contributte
dbname: contributte
mariadb:
driver: mysqli
host: localhost
port: 3306
user: contributte
password: contributte
dbname: contributte

# ======================================
# Services =============================
Expand Down
77 changes: 45 additions & 32 deletions config/doctrine.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@
extensions:
console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)

nettrine.annotations: Nettrine\Annotations\DI\AnnotationsExtension
nettrine.cache: Nettrine\Cache\DI\CacheExtension

nettrine.dbal: Nettrine\DBAL\DI\DbalExtension
nettrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension

nettrine.orm: Nettrine\ORM\DI\OrmExtension
nettrine.orm.cache: Nettrine\ORM\DI\OrmCacheExtension
nettrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension(%consoleMode%)
nettrine.orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension

nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension
nettrine.migrations: Nettrine\Migrations\DI\MigrationsExtension
#nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension
#nettrine.migrations: Nettrine\Migrations\DI\MigrationsExtension

nettrine.dbal:
connection:
driver: %database.driver%
host: %database.host%
port: %database.port%
dbname: %database.dbname%
user: %database.user%
password: %database.password%
charset: UTF8
serverVersion: '15.0'
debug:
panel: %debugMode%
sourcePaths: [%appDir%]

nettrine.fixtures:
paths:
- %appDir%/../db/Fixtures

nettrine.migrations:
directory: %appDir%/../db/Migrations
namespace: DB\Migrations
connections:
default:
driver: %postgres.driver%
host: %postgres.host%
port: %postgres.port%
dbname: %postgres.dbname%
user: %postgres.user%
password: %postgres.password%
charset: UTF8
serverVersion: 15.0.0

second:
driver: %mariadb.driver%
host: %mariadb.host%
port: %mariadb.port%
dbname: %mariadb.dbname%
user: %mariadb.user%
password: %mariadb.password%
charset: UTF8
serverVersion: 10.10.0

nettrine.orm:
entityManagerDecoratorClass: App\Model\Database\EntityManagerDecorator

nettrine.orm.annotations:
mapping:
App\Domain\Database: %appDir%/Domain/Database
managers:
default:
connection: default
mapping:
App:
type: attributes
dirs: [%appDir%/Domain/Database]
namespace: App\Domain\Database
second:
connection: second
mapping:
App:
type: attributes
dirs: [%appDir%/Domain/Database]
namespace: App\Domain\Database

#nettrine.fixtures:
# paths:
# - %appDir%/../db/Fixtures
#
#nettrine.migrations:
# directory: %appDir%/../db/Migrations
# namespace: DB\Migrations
11 changes: 9 additions & 2 deletions config/local.neon.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#parameters:
# database:
# postgres:
# driver: pdo_pgsql
# host: 0.0.0.0
# dbname: contributte
# port: 5432
# user: contributte
# password: contributte
# dbname: contributte
# mariadb:
# driver: mysqli
# host: 0.0.0.0
# port: 3306
# user: contributte
# password: contributte
# dbname: contributte

0 comments on commit 7e59609

Please sign in to comment.