繁体   English   中英

Symfony DDD +事件源与相关对象一起工作

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM