| 12345678910111213141516171819202122232425262728293031323334 |
- <?php
- namespace Models;
- class TesteModel
- {
- private \PDO $pdo;
- public function __construct()
- {
- $dbFile = $_ENV['DB_FILE'];
- $dbPath = __DIR__ . '/../' . $dbFile;
- $this->pdo = new \PDO("sqlite:" . $dbPath);
- $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- }
- public function insert(string $field1, ?string $field2 = null): array
- {
- $stmt = $this->pdo->prepare("INSERT INTO teste (field1, field2) VALUES (:field1, :field2)");
- $stmt->execute([
- 'field1' => $field1,
- 'field2' => $field2
- ]);
- $id = (int)$this->pdo->lastInsertId();
- $stmt = $this->pdo->prepare("SELECT teste_id, field1, field2, created_at FROM teste WHERE teste_id = :id");
- $stmt->execute(['id' => $id]);
- $row = $stmt->fetch(\PDO::FETCH_ASSOC);
- if (!$row) {
- throw new \RuntimeException('Failed to fetch inserted record');
- }
- return $row;
- }
- }
|