$npx -y skills add aligundogdu/symfony-hexagonal-skill --skill symfony-doctrine-persistenceSymfony Doctrine persistence — repository adapters, entity mapping, migrations, transactions, database patterns. Triggers on: doctrine, repository, persistence, database, mapping, migration, ORM, entity manager, DBAL, transaction
| 1 | # Symfony Doctrine Persistence |
| 2 | |
| 3 | You are an expert in Doctrine ORM within Symfony hexagonal architecture. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - User needs a repository implementation |
| 8 | - User asks about entity mapping |
| 9 | - User needs migration management |
| 10 | - User discusses transactions or database access |
| 11 | |
| 12 | ## Core Rules |
| 13 | |
| 14 | 1. **Mapping NEVER in Domain**: No `#[ORM\Entity]` or annotations on domain entities |
| 15 | 2. **Mapping in Infrastructure**: Use XML or separate mapping files in `Infrastructure/{Module}/Persistence/Mapping/` |
| 16 | 3. **Repository = Adapter**: Implements domain port interface |
| 17 | 4. **Event dispatch after persist**: Repository dispatches domain events after flush |
| 18 | 5. **NEVER use native/raw SQL**: No `$connection->executeQuery()`, `$connection->executeStatement()`, `NativeQuery`, `$connection->prepare()`, or raw SQL strings anywhere in application code. Always use Doctrine QueryBuilder (ORM or DBAL), DQL, finder methods, or Criteria API. The only exception is Doctrine Migrations (`$this->addSql()`) which requires raw SQL by design. |
| 19 | |
| 20 | ## Repository Adapter Pattern |
| 21 | |
| 22 | ```php |
| 23 | namespace App\Infrastructure\{Module}\Persistence; |
| 24 | |
| 25 | use App\Domain\{Module}\Entity\{Entity}; |
| 26 | use App\Domain\{Module}\Port\{Entity}RepositoryInterface; |
| 27 | use Doctrine\ORM\EntityManagerInterface; |
| 28 | use Symfony\Component\Messenger\MessageBusInterface; |
| 29 | |
| 30 | final readonly class Doctrine{Entity}Repository implements {Entity}RepositoryInterface |
| 31 | { |
| 32 | public function __construct( |
| 33 | private EntityManagerInterface $entityManager, |
| 34 | private MessageBusInterface $eventBus, |
| 35 | ) { |
| 36 | } |
| 37 | |
| 38 | public function save({Entity} $entity): void |
| 39 | { |
| 40 | $this->entityManager->persist($entity); |
| 41 | $this->entityManager->flush(); |
| 42 | |
| 43 | foreach ($entity->pullDomainEvents() as $event) { |
| 44 | $this->eventBus->dispatch($event); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function findById({Entity}Id $id): ?{Entity} |
| 49 | { |
| 50 | return $this->entityManager->find({Entity}::class, $id->value); |
| 51 | } |
| 52 | |
| 53 | public function remove({Entity} $entity): void |
| 54 | { |
| 55 | $this->entityManager->remove($entity); |
| 56 | $this->entityManager->flush(); |
| 57 | } |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | ## Mapping Strategies |
| 62 | |
| 63 | ### Option A: XML Mapping (Recommended for strict separation) |
| 64 | ```xml |
| 65 | <!-- src/Infrastructure/User/Persistence/Mapping/User.orm.xml --> |
| 66 | <doctrine-mapping> |
| 67 | <entity name="App\Domain\User\Entity\User" table="users"> |
| 68 | <id name="id" type="string" column="id" /> |
| 69 | <embedded name="email" class="App\Domain\User\ValueObject\Email" /> |
| 70 | <field name="name" type="string" /> |
| 71 | <field name="createdAt" type="datetime_immutable" column="created_at" /> |
| 72 | </entity> |
| 73 | </doctrine-mapping> |
| 74 | ``` |
| 75 | |
| 76 | ### Option B: PHP Attributes in Infrastructure |
| 77 | ```php |
| 78 | // Separate mapping class that maps to domain entity |
| 79 | // Configure in doctrine.yaml with mapping paths |
| 80 | ``` |
| 81 | |
| 82 | ## Migration Workflow |
| 83 | |
| 84 | **Always ask the user** which strategy they prefer: |
| 85 | 1. **Auto-diff**: `php bin/console doctrine:migrations:diff` (generates from mapping) |
| 86 | 2. **Manual**: Write migrations by hand for full control |
| 87 | |
| 88 | ## Why No Native SQL? |
| 89 | |
| 90 | Native/raw SQL bypasses Doctrine's abstraction layers and creates several problems: |
| 91 | - **Database portability lost** — Raw SQL ties code to a specific database engine (MySQL, PostgreSQL, etc.) |
| 92 | - **No type safety** — QueryBuilder provides parameter binding with type inference, raw SQL doesn't |
| 93 | - **Mapping bypass** — Entities, value objects, and custom types are ignored when using raw SQL |
| 94 | - **Harder to maintain** — SQL strings scattered in code are harder to refactor than QueryBuilder chains |
| 95 | - **Security risk** — Raw SQL increases the surface for SQL injection if parameters are not properly bound |
| 96 | |
| 97 | ### Detecting Native SQL (for code review) |
| 98 | |
| 99 | Flag these patterns as CRITICAL violations: |
| 100 | ```php |
| 101 | // FORBIDDEN — native SQL patterns |
| 102 | $connection->executeQuery('SELECT ...'); |
| 103 | $connection->executeStatement('INSERT ...'); |
| 104 | $connection->prepare('SELECT ...'); |
| 105 | $connection->exec('DROP ...'); |
| 106 | $entityManager->getConnection()->executeQuery(...); |
| 107 | $entityManager->createNativeQuery(...); |
| 108 | $rsm = new ResultSetMapping(); |
| 109 | |
| 110 | // ALLOWED — Doctrine abstractions |
| 111 | $queryBuilder->select(...)->from(...)->where(...); // DBAL QueryBuilder |
| 112 | $entityManager->createQueryBuilder()->select('u')...; // ORM QueryBuilder |
| 113 | $entityManager->createQuery('SELECT u FROM User u'); // DQL |
| 114 | $repository->findBy([...]); // Finder methods |
| 115 | $repository->matching($criteria); // Criteria API |
| 116 | ``` |
| 117 | |
| 118 | ## References |
| 119 | |
| 120 | See `references/` for detailed guides: |
| 121 | - `repository-patterns.md` — Repository patterns and transaction management |
| 122 | - `mapping-patterns.md` — XML mapping, embeddables, relations |
| 123 | - `migration-workflow.md` — Migration strategies and best practices |
| 124 | - `no-native-sql.md` — Why native SQ |