Winter Doctrine is a module that provides easy configuration and access to Doctrine orm/dbal functionality from WinterBoot applications.
composer require suvera/winter-doctrine
To enable Doctrine module in applications, append following code to application.yml
modules:
- module: dev\winterframework\doctrine\DoctrineModule
enabled: true
in your application.yml file you might already have setup datasources like this.
In below example, there are two datasources configured here with names.
- defaultdb (isPrimary: true)
- admindb
datasource:
- name: defaultdb
isPrimary: true
url: "sqlite::memory:"
username: xxxxx
password: xxzzz
doctrine:
entityPaths:
- /path/to/defaultdb/entities
isDevMode: false
- name: admindb
url: "mysql:host=localhost;port=3307;dbname=testdb"
username: xxxxx
password: xxzzz
doctrine:
entityPaths:
- /path/to/admindb/entities
- /path/other/admindb/entities2
isDevMode: false
driver:
driverOptions:
wrapperClass:
driverClass:
connection:
persistent: true
errorMode: ERRMODE_EXCEPTION
columnsCase: CASE_NATURAL
idleTimeout: 300
autoCommit: true
defaultrowprefetch: 100
ORM/DBAL beans can be Autowired. No need to created them manually.
Bean names are suffixed as following way. Autowired code should input bean name.
Bean Type | Bean Name |
---|---|
ORM EntityManager | {name}-doctrine-em |
ORM Tranaction Manager | {name}-doctrine-emtxn |
DBAL Connection | {name}-doctrine-dbal |
DBAL Tranaction Manager | {name}-doctrine-dbaltxn |
Examples below
// ORM - Primary (defaultdb)
#[Autowired]
private EntityManager $defaultEm;
// Alternatively coded as: #[Autowired("defaultdb-doctrine-em")]
// ORM
#[Autowired("admindb-doctrine-em")]
private EntityManager $adminEm;
// ORM - Primary Tranaction Manager (defaultdb)
#[Autowired]
private EmTransactionManager $defaultTxnManager;
// Alternatively coded as: #[Autowired("defaultdb-doctrine-emtxn")]
// ORM Tranaction Manager
#[Autowired("admindb-doctrine-emtxn")]
private EmTransactionManager $adminTxnManager;
// DBAL Connection - Primary (defaultdb)
#[Autowired]
private Connection $defaultConn;
// Alternatively coded as: #[Autowired("defaultdb-doctrine-dbal")]
// DBAL Connection
#[Autowired("admindb-doctrine-dbal")]
private Connection $adminConn;
// DBAL - Primary Tranaction Manager (defaultdb)
#[Autowired]
private DbalTransactionManager $defaultTxnManager;
// Alternatively coded as: #[Autowired("defaultdb-doctrine-dbaltxn")]
// DBAL Tranaction Manager
#[Autowired("admindb-doctrine-dbaltxn")]
private DbalTransactionManager $adminTxnManager;
Articles About winter-boot framework.
Executing something under ORM/DBAL transaction is pretty easy by just using Transactional annotation
#[Autowired("admindb-doctrine-em")]
private EntityManager $adminEm;
#[Transactional(transactionManager: "admindb-doctrine-emtxn")]
public function executeInTransaction(): void {
// do something here
foreach ($objects as $obj) {
$this->adminEm->persist($obj);
}
// do more things here
}