binsoul/symfony-bundle-doctrine
Installation
composer require binsoul/symfony-bundle-doctrine
Ensure your project uses Symfony 5.4+ (Laravel 8+ via Symfony bridge) or a standalone Symfony app.
Register the Bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel):
return [
// ...
Binsoul\DoctrineBundle\DoctrineBundle::class => ['all' => true],
];
First Use Case: Basic Doctrine Integration
Inject the Doctrine\ORM\EntityManagerInterface into a service/controller:
use Doctrine\ORM\EntityManagerInterface;
class UserController extends AbstractController
{
public function __construct(private EntityManagerInterface $em) {}
public function index()
{
$users = $em->getRepository(User::class)->findAll();
// ...
}
}
Verify Configuration
Check config/packages/doctrine.yaml (auto-generated by the bundle) for DBAL/ORM settings.
Repository Pattern
Extend Doctrine\ORM\EntityRepository for custom queries:
namespace App\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findActiveUsers()
{
return $this->createQueryBuilder('u')
->where('u.isActive = :active')
->setParameter('active', true)
->getQuery()
->getResult();
}
}
Dependency Injection
Bind repositories/services in services.yaml:
services:
App\Repository\UserRepository:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
Lifecycle Callbacks
Use annotations in entities (e.g., @ORM\PrePersist):
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\PrePersist]
public function setCreatedAt(): void
{
$this->createdAt = new \DateTime();
}
}
Migrations Generate migrations via:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
symfony/ux-live-component or symfony/var-dumper for debugging.doctrine.event_dispatcher:
doctrine.event_dispatcher:
listeners:
App\EventListener\UserListener: ~
createQueryBuilder() over DQL for dynamic queries.Entity Manager Scope
EntityManager. Avoid injecting multiple instances.$this->em->getConnection()->getDatabasePlatform()->getName();
Configuration Overrides
doctrine.yaml. Manual edits may be overwritten.doctrine.yaml in config/packages/override/ for customizations.Laravel Eloquent Conflicts
Transaction Management
$this->em->beginTransaction();
try {
$this->em->persist($entity);
$this->em->flush();
$this->em->commit();
} catch (\Exception $e) {
$this->em->rollBack();
throw $e;
}
$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
php bin/console doctrine:schema:validate
Custom DQL Functions
Register in doctrine.yaml:
dbal:
dql:
string_functions:
CONCAT_WS: DoctrineExtensions\Query\Mysql\StringFunctions\ConcatWs
Event Subscribers
Implement Doctrine\Common\EventSubscriber:
class SoftDeleteSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return ['preRemove'];
}
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof SoftDeletable) {
$entity->markAsDeleted();
}
}
}
Custom Types
Extend Doctrine\DBAL\Types\Type and register:
orm:
mappings:
App:
type: attribute
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
is_bundle: false
How can I help you explore Laravel packages today?