Symfony Doctrine Persistence logo

Symfony Doctrine Persistence

Community
aligundogdu
symfony-doctrine-persistence

Doctrine persistence for Symfony hexagonal architecture (ORM 2.x/3.x, DBAL 3/4, DoctrineBundle 2/3): repository adapters, XML mapping in Infrastructure, embeddables, enums, custom DBAL types, native lazy objects, migrations, transactions, read models with DBAL QueryBuilder, strict no-native-SQL rule. Use when: doctrine, repository, persistence, database, mapping, migration, ORM, entity manager, DBAL, transaction, query builder, pagination

Overview

Publisheraligundogdu
Repositorysymfony-hexagonal-skill
Skill namesymfony-doctrine-persistence
Stars
35
Forks
4
Bundled files
4
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 4 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by aligundogdu on GitHub. Read the source before you install it.

Installation

Install the Symfony Doctrine Persistence AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/aligundogdu/symfony-hexagonal-skill/tree/main/skills/symfony-doctrine-persistence
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/aligundogdu/symfony-hexagonal-skill.git /tmp/symfony-hexagonal-skill
mkdir -p .claude/skills
cp -r /tmp/symfony-hexagonal-skill/skills/symfony-doctrine-persistence .claude/skills/symfony-doctrine-persistence
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Symfony Doctrine Persistence in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Symfony Doctrine Persistence on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Symfony Doctrine Persistence is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Symfony Doctrine Persistence

You are an expert in Doctrine ORM within Symfony hexagonal architecture.

When to Activate

  • User needs a repository implementation
  • User asks about entity mapping
  • User needs migration management
  • User discusses transactions or database access

Version Notes

TopicDoctrine ORM 2.x (Symfony 6.4)Doctrine ORM 3.x (Symfony 7.4 / 8.x)
Mapping driversXML, attributes, annotations, YAMLXML and attributes only (annotations + YAML removed)
XML mappingXSD validation optionalXSD validation always on: invalid XML fails at boot
DBAL3.x4.x (Type::getName() removed, register types by name)
Lazy loadingproxy classesPHP 8.4 + ORM 3.4+: native lazy objects (enable_native_lazy_objects: true, DoctrineBundle 2.16+; always on in DoctrineBundle 3.1)
PaginationTools\Pagination\PaginatorORM 3.7+: OffsetPaginator + Window (Paginator deprecated)
Readonly properties / private(set) / hooksreadonly since 2.13readonly; asymmetric visibility + property hooks since 3.4

XML entity mapping is unaffected by Symfony 8 removing XML service/route configuration.

Core Rules

  1. Mapping NEVER in Domain: No #[ORM\Entity] or annotations on domain entities
  2. Mapping in Infrastructure: Use XML or separate mapping files in Infrastructure/{Module}/Persistence/Mapping/
  3. Repository = Adapter: Implements domain port interface
  4. Event dispatch after persist: Repository dispatches domain events after flush, with DispatchAfterCurrentBusStamp
  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.

Repository Adapter Pattern

php
namespace App\Infrastructure\{Module}\Persistence;

use App\Domain\{Module}\Entity\{Entity};
use App\Domain\{Module}\Port\{Entity}RepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;

final readonly class Doctrine{Entity}Repository implements {Entity}RepositoryInterface
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private MessageBusInterface $eventBus,
    ) {
    }

    public function save({Entity} $entity): void
    {
        $this->entityManager->persist($entity);
        $this->entityManager->flush();

        foreach ($entity->pullDomainEvents() as $event) {
            // released after the current command (and its transaction) succeeds
            $this->eventBus->dispatch($event, [new DispatchAfterCurrentBusStamp()]);
        }
    }

    public function findById({Entity}Id $id): ?{Entity}
    {
        return $this->entityManager->find({Entity}::class, $id->value);
    }

    public function remove({Entity} $entity): void
    {
        $this->entityManager->remove($entity);
        $this->entityManager->flush();
    }
}

Mapping Strategies

XML Mapping (required for Domain entities)

File: src/Infrastructure/User/Persistence/Mapping/Entity.User.orm.xml (name = class relative to the mapping prefix, see references/mapping-patterns.md)

xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                  https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="App\Domain\User\Entity\User" table="users">
        <id name="id" type="user_id" column="id" />
        <embedded name="email" class="App\Domain\User\ValueObject\Email" use-column-prefix="false" />
        <field name="name" type="string" />
        <field name="createdAt" type="datetime_immutable" column="created_at" />
    </entity>
</doctrine-mapping>

Why not attributes?

#[ORM\Entity] attributes must sit on the entity class itself, which lives in Domain. XML is the supported driver that keeps the mapping in Infrastructure. Legacy modules may keep attributes until they are refactored.

Migration Workflow

Always ask the user which strategy they prefer:

  1. Auto-diff: php bin/console doctrine:migrations:diff (generates from mapping)
  2. Manual: Write migrations by hand for full control

Why No Native SQL?

Native/raw SQL bypasses Doctrine's abstraction layers and creates several problems:

  • Database portability lost - Raw SQL ties code to a specific database engine (MySQL, PostgreSQL, etc.)
  • No type safety - QueryBuilder provides parameter binding with type inference, raw SQL doesn't
  • Mapping bypass - Entities, value objects, and custom types are ignored when using raw SQL
  • Harder to maintain - SQL strings scattered in code are harder to refactor than QueryBuilder chains
  • Security risk - Raw SQL increases the surface for SQL injection if parameters are not properly bound

Detecting Native SQL (for code review)

Flag these patterns as CRITICAL violations:

php
// FORBIDDEN - native SQL patterns
$connection->executeQuery('SELECT ...');
$connection->executeStatement('INSERT ...');
$connection->prepare('SELECT ...');
$connection->exec('DROP ...');
$entityManager->getConnection()->executeQuery(...);
$entityManager->createNativeQuery(...);
$rsm = new ResultSetMapping();

// ALLOWED - Doctrine abstractions
$queryBuilder->select(...)->from(...)->where(...);    // DBAL QueryBuilder
$entityManager->createQueryBuilder()->select('u')...;  // ORM QueryBuilder
$entityManager->createQuery('SELECT u FROM User u');    // DQL
$repository->findBy([...]);                             // Finder methods
$repository->matching($criteria);                       // Criteria API

References

See references/ for detailed guides:

  • repository-patterns.md - Repository patterns and transaction management
  • mapping-patterns.md - XML mapping, embeddables, relations
  • migration-workflow.md - Migration strategies and best practices
  • no-native-sql.md - Why native SQL is forbidden and how to use QueryBuilder instead

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Symfony Doctrine Persistence AI skill do?

Doctrine persistence for Symfony hexagonal architecture (ORM 2.x/3.x, DBAL 3/4, DoctrineBundle 2/3): repository adapters, XML mapping in Infrastructure, embeddables, enums, custom DBAL types, native lazy objects, migrations, transactions, read models with DBAL QueryBuilder, strict no-native-SQL rule. Use when: doctrine, repository, persistence, database, mapping, migration, ORM, entity manager, DBAL, transaction, query builder, pagination

Why use Symfony Doctrine Persistence on TypingMind?

Because you install it once and use it with any model. Symfony Doctrine Persistence is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Symfony Doctrine Persistence in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/aligundogdu/symfony-hexagonal-skill/tree/main/skills/symfony-doctrine-persistence. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Symfony Doctrine Persistence?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Symfony Doctrine Persistence?

As many as you like. As long as a model supports skills, you can use Symfony Doctrine Persistence with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Symfony Doctrine Persistence AI skill free?

It is published on GitHub by aligundogdu. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇