Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prooph Event Store: Query event stream
<?php
final class Merger
{
// ...
private function getInitialEventFromStream(EmploymentContractId $contractId): EmploymentContractSigned
{
$query = $this->projectionManager->createQuery();
$query
->init(function (): array {
return [];
})
->fromStream('employment_contract_stream')
->when([
EmploymentContractSigned::class => function (
array $state, EmploymentContractSigned $event
) use ($contractId): array {
if ($event->contractId()->sameValueAs($contractId)) {
$state[] = $event;
$this->stop();
}
return $state;
}
])
->run();
return $query->getState()[0];
}
}
@webdevilopers
Copy link
Author

webdevilopers commented Jul 20, 2020

Loading

@fjogeleit
Copy link

fjogeleit commented Jul 20, 2020

A alternative with a Metadatamatcher

<?php

final class Merger
{
    // ...
    private function getInitialEventFromStream(EmploymentContractId $contractId): EmploymentContractSigned
    {
        $metadataMatcher = (new MetadataMatcher())
            ->withMetadataMatch('_aggregate_id', Operator::EQUALS(), $contractId->toString());

        $query = $this->projectionManager->createQuery();
        $query
            ->init(function (): array {
                return [];
            })
            ->fromStream('employment_contract_stream', $metadataMatcher)
            ->when([
                EmploymentContractSigned::class => function (
                    array $state, EmploymentContractSigned $event
                ): array {
                    $state[] = $event;

                    return $state;
                }
            ])
            ->run();

        return $query->getState()[0];
    }
}

Loading

@prolic
Copy link

prolic commented Jul 20, 2020

I think the alternative using a metadata matcher would result in better performance. Other than hand-written manual sql queries, this should be the best approach.

Loading

@webdevilopers
Copy link
Author

webdevilopers commented Jul 23, 2020

Worked like a charm @fjogeleit @prolic! Thank you for this.

Loading

@webdevilopers
Copy link
Author

webdevilopers commented Jul 26, 2020

Update fromStream for Projection, Query and ReadModelProjection:

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment