Skip to content

Commit 551992b

Browse files
authored
Merge pull request #6634 from acoumb/main
Update UIBuilder related collection documentation
2 parents 8aed0d3 + a5ba5d0 commit 551992b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

13/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

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

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

0 commit comments

Comments
 (0)