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

时间:2019-01-09 14:04:21

标签: symfony events doctrine domain-driven-design

我用于项目样板:https://github.com/jorge07/symfony-4-es-cqrs-boilerplate

我在保存和获取具有相关字段的对象时遇到了一些问题,但有一些例子:

  • 用户

流具有所有者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>

我想创建与用户有关的新流。因此,我创建了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 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对象,因为主义认为用户是新对象,并尝试级联持久化。

处理关系的正确方法是什么?

抱歉,我的英语;)

1 个答案:

答案 0 :(得分:0)

可能为时已晚,但是我认为您的问题来自于将StreamWasCreated对象反序列化的方法。

您返回类型为StreamWasCreated的对象,该对象必须以UserViewInterface作为第二个参数,因此我们必须确保$data['user']包含UserViewInterface。 / p>