我用于我的项目样板:https://github.com/jorge07/symfony-4-es-cqrs-boilerplate
我在使用相关字段保存和获取对象时遇到了一些问题,但是有一些例子:
Stream拥有所有者User,因此映射:
流
<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>
我想创建与User相关的新流。所以,我创建了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);
}
create method from Stream
public static function create(UuidInterface $uuid, UserViewInterface $user, Parameters $parameters): self
{
$stream = new self();
$stream->apply(new StreamWasCreated($uuid, $user, $parameters));
return $stream;
}
和StreamWasCreated事件
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()
];
}
在这一步我没有序列化用户,一切都很好。但是......当我想获得这条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对象,因为doctrine认为用户是新对象并尝试级联持久化。
处理关系的正确方法是什么?
对不起我的英文;)
可能为时已晚,但我认为您的问题来自于反序列化StreamWasCreated
对象的方法。
你返回一个StreamWasCreated
类型的对象,它必须将UserViewInterface
作为第二个参数,所以我们必须确保$data['user']
包含UserViewInterface
。