Skip to content

Commit d830e88

Browse files
committed
Add #6634 for 14
1 parent 16834e0 commit d830e88

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

14/umbraco-ui-builder/advanced/repositories.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public class PersonRepository : Repository<Person, int> {
4545
protected override long GetCountImpl(Expression<Func<Person, bool>> whereClause) {
4646
...
4747
}
48+
49+
protected override IEnumerable<TJunctionEntity> GetRelationsByParentIdImpl<TJunctionEntity>(int parentId, string relationAlias)
50+
{
51+
...
52+
}
53+
54+
protected override TJunctionEntity SaveRelationImpl<TJunctionEntity>(TJunctionEntity entity)
55+
{
56+
...
57+
}
4858
}
4959
````
5060

14/umbraco-ui-builder/collections/related-collections.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class StudentCourse
7272
## Defining a related collection
7373

7474
You can get started with related collection through a two step process:
75+
7576
1. Add collection definition
7677
2. Add related collection entity picker and definition
7778

@@ -118,3 +119,48 @@ collectionConfig.Editor(editorConfig =>
118119
{% hint style="info" %}
119120
**Relation Config Alias:** The relation config alias must correspond to the related collection picker field alias! (e.g. `studentsCourses`)
120121
{% endhint %}
122+
123+
## Defining repository methods
124+
125+
### **IEnumerable<StudentCourse> GetRelationsByParentIdImpl<StudentCourse>(int parentId, string relationAlias)**
126+
127+
Retrieves the related collections based on the ID of the parent entity.
128+
129+
```csharp
130+
{
131+
var db = _scopeProvider.CreateScope().Database;
132+
var sql = db.SqlContext.Sql()
133+
.Select(new[] { "StudentId", "CourseId" } )
134+
.From("StudentsCourses")
135+
.Where($"studentId = @0", parentId);
136+
137+
var result = db.Fetch<StudentCourse>(sql);
138+
139+
return result;
140+
}
141+
```
142+
143+
### **StudentCourse SaveRelationImpl<StudentCourse>(StudentCourse entity)**
144+
145+
Adds a new related collection to the current parent entity.
146+
147+
```csharp
148+
{
149+
var db = _scopeProvider.CreateScope().Database;
150+
151+
var type = entity.GetType();
152+
var studentId = type.GetProperty("StudentId").GetValue(entity);
153+
var courseId = type.GetProperty("CourseId").GetValue(entity);
154+
155+
// delete relation if exists
156+
db.Execute("DELETE FROM StudentsCourses WHERE StudentId = @0 AND CourseId = @1",
157+
studentId,
158+
courseId);
159+
160+
db.Execute("INSERT INTO StudentsCourses (StudentId, CourseId) VALUES (@0, @1)",
161+
studentId,
162+
courseId);
163+
164+
return entity;
165+
}
166+
```

0 commit comments

Comments
 (0)