[英]Symfony DDD + Event Sourcing work with related object
我為我的項目樣板使用: https : //github.com/jorge07/symfony-4-es-cqrs-boilerplate
我在與相關字段進行保存和獲取對象時遇到一些問題,但有一些示例:
流具有所有者用戶,因此映射:
流
<entity name="App\Infrastructure\Stream\Query\Projections\StreamView" table="streams">
<id name="uuid" type="uuid_binary" column="uuid"/>
(...)
<many-to-one field="user" target-entity="App\Infrastructure\User\Query\Projections\UserView" inversed-by="streams">
<join-column nullable="false" referenced-column-name="uuid" />
</many-to-one>
</entity>
用戶
<entity name="App\Infrastructure\User\Query\Projections\UserView" table="users">
<id name="uuid" type="uuid_binary" column="uuid"/>
(...)
<one-to-many field="streams" target-entity="App\Infrastructure\Stream\Query\Projections\StreamView" mapped-by="user">
</one-to-many>
</entity>
我想創建與用戶相關的新流。 因此,我創建了CreateStreamCommand和Handler,如下所示:
public function __invoke(CreateStreamCommand $command): void
{
$stream = $this->streamFactory->register($command->uuid, $command->user, $command->parameters);
$this->streamRepository->store($stream);
}
public function __construct(StreamFactory $streamFactory, StreamRepositoryInterface $streamRepository)
{
$this->streamFactory = $streamFactory;
$this->streamRepository = $streamRepository;
}
從StreamFactory注冊方法
public function register(UuidInterface $uuid, UserViewInterface $user, Parameters $parameters): Stream
{
return Stream::create($uuid, $user, $parameters);
}
從Stream創建方法
public function __construct(UuidInterface $uuid, UserViewInterface $user, Parameters $parameters)
{
$this->uuid = $uuid;
$this->user = $user;
$this->parameters = $parameters;
}
/**
* @throws \Assert\AssertionFailedException
*/
public static function deserialize(array $data): self
{
Assertion::keyExists($data, 'uuid');
Assertion::keyExists($data, 'user');
Assertion::keyExists($data, 'parameters');
return new self(
Uuid::fromString($data['uuid']),
$data['user'],
Parameters::fromArray($data['parameters'])
);
}
public function serialize(): array
{
return [
'uuid' => $this->uuid->toString(),
'user' => $this->user,
'parameters' => $this->parameters->toArray()
];
}
和StreamWasCreated事件
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Argument 2 passed to App\Domain\Stream\Event\StreamWasCreated::__construct() must implement interface App\Domain\User\Query\Projections\UserViewInterface, array given, called in /app/src/Domain/Stream/Event/StreamWasCreated.php on line 51 in /app/src/Domain/Stream/Event/StreamWasCreated.php:32
Stack trace:
#0 /app/src/Domain/Stream/Event/StreamWasCreated.php(51): App\Domain\Stream\Event\StreamWasCreated->__construct(Object(Ramsey\Uuid\Uuid), Array, Object(App\Domain\Stream\ValueObject\Parameters))
#1 /app/vendor/broadway/broadway/src/Broadway/Serializer/SimpleInterfaceSerializer.php(58): App\Domain\Stream\Event\StreamWasCreated::deserialize(Array)
#2 /app/vendor/broadway/event-store-dbal/src/DBALEventStore.php(234): Broadway\Serializer\SimpleInterfaceSerializer->deserialize(Array)
#3 /app/vendor/broadway/event-store-dbal/src/DBALEventStore.php(93): Broadway\EventStore\Dbal\DBALEventStore->deserializeEvent(Array)
#4 /app/vendor/broadway/broadway/s in /app/src/Domain/Stream/Event/StreamWasCreated.php on line 32
在這一步,我不序列化用戶,一切都很好。 但是...當我想要獲取此Stream記錄時,看到錯誤:
PHP Fatal error: Uncaught Symfony\\Component\\Debug\\Exception\\FatalThrowableError: Argument 2 passed to App\\Domain\\Stream\\Event\\StreamWasCreated::__construct() must implement interface App\\Domain\\User\\Query\\Projections\\UserViewInterface, array given, called in /app/src/Domain/Stream/Event/StreamWasCreated.php on line 51 in /app/src/Domain/Stream/Event/StreamWasCreated.php:32 Stack trace: #0 /app/src/Domain/Stream/Event/StreamWasCreated.php(51): App\\Domain\\Stream\\Event\\StreamWasCreated->__construct(Object(Ramsey\\Uuid\\Uuid), Array, Object(App\\Domain\\Stream\\ValueObject\\Parameters)) #1 /app/vendor/broadway/broadway/src/Broadway/Serializer/SimpleInterfaceSerializer.php(58): App\\Domain\\Stream\\Event\\StreamWasCreated::deserialize(Array) #2 /app/vendor/broadway/event-store-dbal/src/DBALEventStore.php(234): Broadway\\Serializer\\SimpleInterfaceSerializer->deserialize(Array) #3 /app/vendor/broadway/event-store-dbal/src/DBALEventStore.php(93): Broadway\\EventStore\\Dbal\\DBALEventStore->deserializeEvent(Array) #4 /app/vendor/broadway/broadway/s in /app/src/Domain/Stream/Event/StreamWasCreated.php on line 32
我檢查了第二個參數,它是空數組。 因此,我認為問題在於反序列化事件數據。
因此,我的第二次嘗試是序列化User對象,但是后來我無法保存Stream對象,因為主義認為用戶是新對象,並嘗試級聯持久化。
處理關系的正確方法是什么?
對不起,我的英語;)
可能為時已晚,但我認為您的問題來自於反序列化StreamWasCreated
對象的方法。
您返回StreamWasCreated
類型的對象,該對象必須將UserViewInterface
作為第二個參數,因此我們必須確保$data['user']
包含UserViewInterface
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.