Doctrine orm 条令实体未包含在条令事件回调中
我有一对多关系的实体Doctrine orm 条令实体未包含在条令事件回调中,doctrine-orm,doctrine,symfony,Doctrine Orm,Doctrine,Symfony,我有一对多关系的实体 TransactionItem--有许多-->付款 编辑:包括映射文件 Payment.yml映射文件 Akademia\Institution\Sales\Domain\Model\Payment\Payment: type: entity table: payments repositoryClass: Akademia\Institution\Sales\Infrastructure\Persistence\Doctrine\Payment\D
TransactionItem--有许多-->付款
编辑:包括映射文件
Payment.yml映射文件
Akademia\Institution\Sales\Domain\Model\Payment\Payment:
type: entity
table: payments
repositoryClass: Akademia\Institution\Sales\Infrastructure\Persistence\Doctrine\Payment\DoctrinePaymentRepository
id:
id:
type: string
length: 36
fields:
date:
type: datetime
amount:
type: decimal
precision: 11
scale: 2
manyToOne:
transactionItem:
targetEntity: Akademia\Institution\Sales\Domain\Model\TransactionItem
inversedBy: payments
joinColumn:
name: transaction_item_id
referencedColumnName: id
TransactionItem.yml映射
Akademia\Institution\Sales\Domain\Model\TransactionItem:
type: entity
table: transaction_items
repositoryClass: Akademia\Institution\Sales\Infrastructure\Persistence\Doctrine\DoctrineTransactionItemRepository
id:
id:
type: string
length: 36
fields:
productId:
type: string
column: product_id
productType:
type: string
column: product_type
description:
type: string
quantity:
type: integer
price:
type: decimal
precision: 11
scale: 2
discount:
type: decimal
precision: 11
scale: 2
paid:
type: boolean
remarks:
type: string
nullable: true
manyToOne:
transaction:
targetEntity: Akademia\Institution\Sales\Domain\Model\Transaction
inversedBy: lineItems
joinColumn:
name: transaction_id
referencedColumnName: id
oneToMany:
payments:
targetEntity: Akademia\Institution\Sales\Domain\Model\Payment\Payment
mappedBy: transactionItem
cascade: ['persist']
fetch: EAGER
我打电话的时候
$transactionItem->pay($amount, $date);
$entityManager->persist($transactionItem);
$entityManager->flush();
TransactionItem.php
public function pay($amount, \DateTime $date = null)
{
$payment = new Payment(
Uuid::uuid4()->toString(),
$this,
$amount,
$date ?? new \DateTime());
$this->payments[] = $payment;
}
我希望我可以在这里获得关于条令回调的TransactionItem
:
服务.yml
services:
app.doctrine_event_listener:
class: AppBundle\Event\DomainEventListener
arguments: ["@app.domain_event_dispatcher"]
tags:
- { name: doctrine.event_listener, event: postPersist }
- { name: doctrine.event_listener, event: postUpdate }
- { name: doctrine.event_listener, event: postRemove }
- { name: doctrine.event_listener, event: postFlush }
app.domain_event_dispatcher:
class: AppBundle\Event\DefaultEventDispatcher
听众:
class DomainEventListener
{
private $entities = array();
/** @var DomainEventDispatcher */
private $dispatcher;
public function __construct(DomainEventDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function postPersist($event)
{
$this->keepAggregateRoots($event);
}
public function postUpdate($event)
{
$this->keepAggregateRoots($event);
}
public function postRemove($event)
{
$this->keepAggregateRoots($event);
}
public function postFlush($event)
{
/** @var AggregateRoot $entity */
VarDumper::dump($this->entities);die();
foreach ($this->entities as $entity) {
foreach ($entity->releaseEvents() as $anEvent) {
$this->dispatcher->dispatch($anEvent);
}
}
$this->entities = array();
}
private function keepAggregateRoots($event)
{
$entity = $event->getEntity();
if (!($entity instanceof AggregateRoot)) {
return;
}
$this->entities[] = $entity;
}
}
但是在postFlushVarDumper::dump($this->entities)上转储捕获的实体时代码>
我只是得到一个空数组结果
注:
TransactionItem
扩展了AggregateRoot
,但付款不扩展
但至少我希望TransactionItem
在场
仅仅添加子关系不会将其包含在条令事件中吗?
如何手动告诉条令,TransactionItem
应该出现在postFlush事件中?
编辑:未调用在postUpdate内部转储。
public function postUpdate($event)
{
$this->keepAggregateRoots($event);
VarDumper::dump('Inside postUpdate');
VarDumper::dump($this->entities);die();
}
是否确实调用了这些方法?你似乎在创建一个事件订阅者,而不是我的监听器。我想是这样的,因为在postPersist事件中调用VarDumper::dump('Im inside postPersist')
与在postFlush中发布的代码一样有效。我将编辑帖子以包含services.yml配置,事件postFlush
是否包含实体?否,我得到的是空数组。即使在postPersist中转储也会导致空数组。我怀疑这是因为我只是在TransactionItem上添加子实体,而没有更改任何其他字段(只是猜测),我认为你是对的。未调用postUpdate。但是为什么?你确定这些方法都被调用了吗?你似乎在创建一个事件订阅者,而不是我的监听器。我想是这样的,因为在postPersist事件中调用VarDumper::dump('Im inside postPersist')
与在postFlush中发布的代码一样有效。我将编辑帖子以包含services.yml配置,事件postFlush
是否包含实体?否,我得到的是空数组。即使在postPersist中转储也会导致空数组。我怀疑这是因为我只是在TransactionItem上添加子实体,而没有更改任何其他字段(只是猜测),我认为你是对的。未调用postUpdate。但是为什么呢?