我尝试将更改保存在Symfony中实体的Changlog表中。由于性能原因,Changlog表具有自己的数据库。
如何在生命周期回调方法中访问其他EntityManager?
我试图通过PreUpdateEventArgs访问EntityManager,但是只有当前Entity的EntityManager可用。 注入ContainerInterface也不起作用。
不幸的是,文档没有帮助我,也没有通过Google找到任何可比较的示例,因此,我非常感谢您的帮助。
我的配置:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '4.2.13.3'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
log:
driver: 'pdo_mysql'
server_version: '4.2.13.3'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_LOG_URL)%'
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
#auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
filters:
is_deleted: App\Doctrine\IsDeletedFilter
log:
connection: log
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Log:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Log/Entity'
prefix: 'App\Log\Entity'
alias: Log
我的实体:
...
namespace App\Entity;
...
use \App\Log\Entity\Changelog;
...
/**
* @ORM\PreUpdate
*/
public function setUpdatedAtValue(\Doctrine\ORM\Event\PreUpdateEventArgs $eventArgs)
{
$this->udate = new \DateTime();
$changeLog = new Changelog;
$changeLog->setCrdate(new \DateTime());
$changeLog->setChangeid($this->id);
$changeLog->setChangedata($eventArgs->getentityChangeSet());
$entityManager = $eventArgs->getEntityManager('log'); // does not work
$entityManager->persist($changeLog);
$entityManager->flush();
}
}