I have Subscription
entity, which represents one of many-to-many relations between users in Twitter-like following model. It has surrogate ID to ensure the uniqueness of relation, which is represented by Value Object SubscriptionId
. SubscriptionId
is persisted as a MongoBinData
. Here is the XML description:
<document name="Subscription"
collection="users.subscriptions"
repository-class="SubscriptionRepository">
<field name="id" id="true" type="User.Subscription.Id"/>
<field name="u" fieldName="userId" type="User.Id"/>
<field name="t" fieldName="subscribedTo" type="User.Id"/>
<field name="d" fieldName="dateTime" type="PreciseDateTime"/>
</document>
I set id
field to Subscription
in constructor manually. The problem is that ODM forces upsert operation instead of insert for new entities with IDs. Thus I have no chance to detect duplication (domain events will be raised twice, etc.). This code executes without duplicate exception:
$sub = new Subscription($subscriberId, $subscribedToId);
$dm->persist($sub);
$dm->flush();
$dm->clear();
$dm->persist($sub);
$dm->flush();
I tried to use custom id generator instead of setting id
in constructor. But for some reason ODM ignored my type="User.Subscription.Id"
and treated ID type as custom_id
. My entity had instance of MongoBinData
instead of desired SubscriptionId
.
I'm looking for a graceful way to achieve the goals:
- VO as entity's id
- Duplication exception when persisting new entity with existing ID