# Datenbank-Konfiguration

## Datenbankverbindung einrichten

### 1. Umgebungsvariablen konfigurieren

Erstellen Sie eine `.env.local` Datei im Projektverzeichnis:

```env
# Datenbank
DATABASE_URL="mysql://admin:hyc6auz4@192.168.1.4:3306/fleet_monitoring"

# Datalogger API
DATALOGGER_API_BASE_URL=http://192.168.1.4:8080

# Symfony
APP_ENV=dev
APP_SECRET=your-secret-key-change-this-in-production
```

**Wichtig:** Die `.env.local` Datei wird nicht in Git eingecheckt (siehe `.gitignore`).

### 2. Datenbankverbindung testen

Nach der Installation von Composer und Doctrine:

```powershell
php bin/console doctrine:schema:validate
```

### 3. Datenbankschema erstellen

Falls Sie Entities haben, können Sie das Schema erstellen:

```powershell
# Schema erstellen (ohne Migration)
php bin/console doctrine:schema:create

# Oder Migration erstellen
php bin/console make:migration
php bin/console doctrine:migrations:migrate
```

## Datenbank-URL Format

Die `DATABASE_URL` folgt diesem Format:

```
mysql://[user]:[password]@[host]:[port]/[database]
```

### Beispiele

**Lokale MySQL:**
```
DATABASE_URL="mysql://root:password@127.0.0.1:3306/fleet_monitoring"
```

**Remote MySQL (Ihr Fall):**
```
DATABASE_URL="mysql://admin:hyc6auz4@192.168.1.4:3306/fleet_monitoring"
```

**Mit SSL:**
```
DATABASE_URL="mysql://user:pass@host:3306/db?serverVersion=8.0&sslMode=REQUIRED"
```

## Doctrine ORM

Doctrine ist bereits konfiguriert in:
- `config/packages/doctrine.yaml`
- `config/packages/doctrine_migrations.yaml`

### Entities erstellen

```powershell
php bin/console make:entity
```

### Repository erstellen

```powershell
php bin/console make:repository
```

### Migration erstellen

```powershell
php bin/console make:migration
php bin/console doctrine:migrations:migrate
```

## Nützliche Doctrine-Befehle

```powershell
# Schema validieren
php bin/console doctrine:schema:validate

# Schema aktualisieren (Vorsicht: kann Daten löschen!)
php bin/console doctrine:schema:update --force

# Migration Status
php bin/console doctrine:migrations:status

# Letzte Migration rückgängig machen
php bin/console doctrine:migrations:migrate prev

# SQL für Migrationen anzeigen (ohne Ausführung)
php bin/console doctrine:migrations:migrate --dry-run
```

## Troubleshooting

### Verbindungsfehler

1. Prüfen Sie, ob MySQL läuft
2. Prüfen Sie Firewall-Einstellungen
3. Testen Sie die Verbindung mit einem MySQL-Client
4. Prüfen Sie Benutzername und Passwort

### "Access denied" Fehler

- Stellen Sie sicher, dass der Benutzer `admin` existiert
- Prüfen Sie, ob der Benutzer Zugriff auf die Datenbank `fleet_monitoring` hat
- Prüfen Sie, ob Remote-Zugriff erlaubt ist (falls remote)

### "Unknown database" Fehler

Erstellen Sie die Datenbank:

```sql
CREATE DATABASE fleet_monitoring CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

Oder mit MySQL-Client:

```powershell
mysql -h 192.168.1.4 -u admin -p
# Dann: CREATE DATABASE fleet_monitoring;
```



