Answer the question
In order to leave comments, you need to log in
What is the correct way to make a 1:M relationship in an aggregate when using DDD?
Tell me how to properly organize a one-to-many relationship in the Aggregate between entities in ORM (for example, in Doctrine 2).
Let's take an example: there is a Rule ( Rule ) and Violation ( Violation ).
It is obvious that there can be no Violation without a Rule. Rule is an Aggregate Root, i.e. we will access Violation only through Rule.
The question is, how to make a relationship here?
Options:
class Violation
{
public function __construct(ViolationId $vid, RuleId $rid, /* other parameters /*)
}
class Violation
{
public function __construct(ViolationId $vid, /* other parameters /*)
}
Answer the question
In order to leave comments, you need to log in
rewrite the question in Russian, please, what
organize a one-to-many relationship in an Aggregate(there is the concept of Aggregation, but I hear about Aggregates for the first time)
be accessible(a song without words, a night without sleep..)
You can read about setting up Doctrine in relation to DDD here: https://leanpub.com/ddd-in-php. On the whole, a very useful book.
Why do you need interger? Why do you need to pass an ID to the constructor? This is DDD, you work with objects and their behavior, not properties of objects.
I.e
<?php
class Violation
{
private $id;
private $rules;
public function __construct(ViolationID $vid, array $rules = []) {
$this->id = $vid;
$rules = $rules;
}
public function addRule(Rule $rule) {
$this->rules[] = $rule;
return $this;
}
public function deleteRule(Rule $rile)...
public function getRules()...
...
}
class Rule
{
private $id;
private $violation;
public function __construct(RuleID $rid, Violation $violation) {
$this->id = $rid;
$this->violation = $violation;
}
...
}
Core\Violation:
type: entity
table: violation
repositoryClass: InfrastructureBundle\Repository\PersistViolationRepository
oneToMany:
rules:
cascade: [ "persist", "remove" ]
targetEntity: Core\Rule
mappedBy: violation
orphanRemoval: true
Core\Rule:
type: entity
table: rule
repositoryClass: InfrastructureBundle\Repository\PersistRuleRepository
manyToOne:
violation:
targetEntity: Core\Violation
inversedBy: rules
joinColumn:
name: violation_id
referencedColumnName: id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question