Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 13/umbraco-ui-builder/advanced/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ public class PersonRepository : Repository<Person, int> {
protected override long GetCountImpl(Expression<Func<Person, bool>> whereClause) {
...
}

protected override IEnumerable<TJunctionEntity> GetRelationsByParentIdImpl<TJunctionEntity>(int parentId, string relationAlias)
{
...
}

protected override TJunctionEntity SaveRelationImpl<TJunctionEntity>(TJunctionEntity entity)
{
...
}
}
````

Expand Down
46 changes: 46 additions & 0 deletions 13/umbraco-ui-builder/collections/related-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,49 @@ collectionConfig.Editor(editorConfig =>
{% hint style="info" %}
**Relation Config Alias:** The relation config alias must correspond to the related collection picker field alias! (e.g. `studentsCourses`)
{% endhint %}

## Defining repository methods

### **IEnumerable<StudentCourse> GetRelationsByParentIdImpl<StudentCourse>(int parentId, string relationAlias)**

Retrieves the related collections based on the ID of the parent entity.

```csharp
{
var db = _scopeProvider.CreateScope().Database;
var sql = db.SqlContext.Sql()
.Select(new[] { "StudentId", "CourseId" } )
.From("StudentsCourses")
.Where($"studentId = @0", parentId);

var result = db.Fetch<StudentCourse>(sql);

return result;
}
```

### **StudentCourse SaveRelationImpl<StudentCourse>(StudentCourse entity)**

Adds a new related collection to the current parent entity.

```csharp
{
var db = _scopeProvider.CreateScope().Database;

var type = entity.GetType();
var studentId = type.GetProperty("StudentId").GetValue(entity);
var courseId = type.GetProperty("CourseId").GetValue(entity);

// delete relation if exists
db.Execute("DELETE FROM StudentsCourses WHERE StudentId = @0 AND CourseId = @1",
studentId,
courseId);

db.Execute("INSERT INTO StudentsCourses (StudentId, CourseId) VALUES (@0, @1)",
studentId,
courseId);

return entity;
}
```

Loading