从 Doctrine 迁移
Weaver ORM ships a compatibility bridge that lets you migrate an existing Doctrine ORM codebase incrementally. The bridge exposes the familiar Doctrine EntityManager API as a thin, deprecated wrapper over the Weaver internals so that existing code continues to compile and run while you migrate file by file.
DoctrineCompatEntityManager
Weaver\ORM\Bridge\Doctrine\DoctrineCompatEntityManager wraps EntityWorkspace and forwards all Doctrine EntityManager calls to their Weaver equivalents. Every method on this wrapper is tagged @deprecated to guide migration.
<?php
use Weaver\ORM\Bridge\Doctrine\DoctrineCompatEntityManager;
// Register as the 'doctrine.orm.entity_manager' alias in Symfony:
// config/services.yaml
// Doctrine\ORM\EntityManagerInterface: '@Weaver\ORM\Bridge\Doctrine\DoctrineCompatEntityManager'
class LegacyOrderService
{
public function __construct(
private readonly \Doctrine\ORM\EntityManagerInterface $em, // still works
) {}
public function createOrder(array $data): Order
{
$order = new Order($data);
$this->em->persist($order); // calls workspace->add() internally
$this->em->flush(); // calls workspace->push() internally
return $order;
}
}
Method mapping
| Doctrine method | Weaver equivalent | Notes |
|---|---|---|
persist($entity) | add($entity) | Marks entity for INSERT |
flush() | push() | Executes all pending SQL |
remove($entity) | delete($entity) | Marks entity for DELETE |
refresh($entity) | reload($entity) | Reloads from database |
detach($entity) | untrack($entity) | Removes from tracking |
contains($entity) | isTracked($entity) | Returns bool |
clear() | reset() | Clears identity map |
find($class, $id) | $repo->find($id) | Use repository directly |
getRepository($class) | $repo (DI) | Inject repository instead |
beginTransaction() | $txManager->begin() | Via TransactionManager |
commit() | $txManager->commit() | Via TransactionManager |
rollback() | $txManager->rollback() | Via TransactionManager |
wrapInTransaction(fn) | $txManager->run(fn) | Recommended approach |
Lifecycle attribute mapping
Doctrine lifecycle attributes are remapped transparently through the bridge. Your existing entity classes do not need to change.
| Doctrine attribute | Weaver attribute | Event timing |
|---|---|---|
#[PrePersist] | #[BeforeAdd] | Before INSERT |
#[PostPersist] | #[AfterAdd] | After INSERT |
#[PreUpdate] | #[BeforeUpdate] | Before UPDATE |
#[PostUpdate] | #[AfterUpdate] | After UPDATE |
#[PreRemove] | #[BeforeDelete] | Before DELETE |
#[PostRemove] | #[AfterDelete] | After DELETE |
#[PostLoad] | #[AfterLoad] | After hydration |