Created
June 1, 2017 11:22
-
-
Save bwaidelich/8c86b016eefb52b056272fb8eea61794 to your computer and use it in GitHub Desktop.
Flow EventSourcing: Use Value Objects as Event Payload / ReadModel primary keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Neos: | |
Flow: | |
persistence: | |
doctrine: | |
dbal: | |
mappingTypes: | |
'user_id': | |
dbType: string | |
className: 'Some\Package\UserIdDbalType' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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