Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created June 1, 2017 11:22
Show Gist options
  • Save bwaidelich/8c86b016eefb52b056272fb8eea61794 to your computer and use it in GitHub Desktop.
Save bwaidelich/8c86b016eefb52b056272fb8eea61794 to your computer and use it in GitHub Desktop.
Flow EventSourcing: Use Value Objects as Event Payload / ReadModel primary keys
<?php
namespace Some\Package
use Neos\EventSourcing\Event\EventInterface;
final class ExampleEvent implements EventInterface
{
/**
* @var UserId
*/
private $userId;
public function __construct(UserId $userId)
{
$this->userId = $userId;
}
public function getUserId(): UserId
{
return $this->userId;
}
}
Neos:
Flow:
persistence:
doctrine:
dbal:
mappingTypes:
'user_id':
dbType: string
className: 'Some\Package\UserIdDbalType'
<?php
namespace Some\Package
use Doctrine\ORM\Mapping as ORM;
use Neos\EventSourcing\Annotations as ES;
use Neos\Flow\Annotations as Flow;
/**
* @Flow\Entity
* @ES\ReadModel
*/
class User
{
/**
* @var UserId
* @ORM\Id
* @ORM\Column(type="user_id")
*/
protected $id;
public function __construct(UserId $id)
{
$this->id = $id;
}
public function getId(): UserId
{
return $this->id;
}
}
<?php
namespace Some\Package;
use Neos\Flow\Annotations as Flow;
/**
* @Flow\Proxy(false)
*/
final class UserId implements \JsonSerializable
{
/**
* @var string
*/
private $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function jsonSerialize(): string
{
return $this->value;
}
public function __toString(): string
{
return $this->value;
}
}
<?php
namespace Some\Package;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
final class UserIdDbalType extends Type
{
public function getName()
{
return 'user_id';
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return (null === $value) ? null : new UserId($value);
}
public function getDefaultLength(AbstractPlatform $platform)
{
return 40;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment