[英]Symfony DDD + Event Sourcing work with related object
I use for my project boilerplate: https://github.com/jorge07/symfony-4-es-cqrs-boilerplate 我为我的项目样板使用: https : //github.com/jorge07/symfony-4-es-cqrs-boilerplate
I have some problems with save and fetch object with related field, but some example: 我在与相关字段进行保存和获取对象时遇到一些问题,但有一些示例:
Stream has owner User, so mapping: 流具有所有者用户,因此映射:
stream 流
<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>
user 用户
<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>
I want to create new stream, related to User. 我想创建与用户相关的新流。 So, I create CreateStreamCommand and Handler, like this:
因此,我创建了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;
}
register method from StreamFactory 从StreamFactory注册方法
public function register(UuidInterface $uuid, UserViewInterface $user, Parameters $parameters): Stream
{
return Stream::create($uuid, $user, $parameters);
}
create method from 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()
];
}
And StreamWasCreated Event 和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
At this step I don't serialize user, and everything is fine. 在这一步,我不序列化用户,一切都很好。 But... When I want to get this Stream record, I see error:
但是...当我想要获取此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
I checked second argument, and it is empty array. 我检查了第二个参数,它是空数组。 So I think, problem is in unserialize event data.
因此,我认为问题在于反序列化事件数据。
So my second try was serialize User object, but then I can't save Stream object, because doctrine thinks user is new object and try to cascade persist. 因此,我的第二次尝试是序列化User对象,但是后来我无法保存Stream对象,因为主义认为用户是新对象,并尝试级联持久化。
What is the propper way to work with relationship? 处理关系的正确方法是什么?
Sorry my english ;) 对不起,我的英语;)
it may be too late, but I think your problem comes from your method that deserializes the StreamWasCreated
object. 可能为时已晚,但我认为您的问题来自于反序列化
StreamWasCreated
对象的方法。
You return an object of the type StreamWasCreated
that must take a UserViewInterface
as a second parameter, so we have to make sure that $data['user']
contains a UserViewInterface
. 您返回
StreamWasCreated
类型的对象,该对象必须将UserViewInterface
作为第二个参数,因此我们必须确保$data['user']
包含UserViewInterface
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.