From a5ba5d06ef64d56acdb6b96f0e515659cd7d0e35 Mon Sep 17 00:00:00 2001 From: Adrian Cojocariu Date: Thu, 7 Nov 2024 14:55:23 +0200 Subject: [PATCH] Update UIBuilder related collection documentation --- .../advanced/repositories.md | 10 ++++ .../collections/related-collections.md | 46 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/13/umbraco-ui-builder/advanced/repositories.md b/13/umbraco-ui-builder/advanced/repositories.md index a460ef49b59..f49db720184 100644 --- a/13/umbraco-ui-builder/advanced/repositories.md +++ b/13/umbraco-ui-builder/advanced/repositories.md @@ -45,6 +45,16 @@ public class PersonRepository : Repository { protected override long GetCountImpl(Expression> whereClause) { ... } + + protected override IEnumerable GetRelationsByParentIdImpl(int parentId, string relationAlias) + { + ... + } + + protected override TJunctionEntity SaveRelationImpl(TJunctionEntity entity) + { + ... + } } ```` diff --git a/13/umbraco-ui-builder/collections/related-collections.md b/13/umbraco-ui-builder/collections/related-collections.md index 5d9effa73dc..69dbaf158d4 100644 --- a/13/umbraco-ui-builder/collections/related-collections.md +++ b/13/umbraco-ui-builder/collections/related-collections.md @@ -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 GetRelationsByParentIdImpl(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(sql); + + return result; +} +``` + +### **StudentCourse SaveRelationImpl(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; +} +``` +