Nibble

UI-O-Matic and one to many relations

Just had this question on the forum and makes sense to share this in a blog post so here it is

How can you handle one to many relations with UI-O-Matic? Think order and orderlines

If you take a look at the example project we have the following 2 classes

Dog

1 [UIOMatic(Dogs, icon-users, icon-user, RenderType = UIOMaticRenderType.List)] 2 [TableName(Dogs)] 3 public class Dog : IUIOMaticModel 4 { 5 public Dog() { } 6 7 [UIOMaticIgnoreField] 8 [PrimaryKeyColumn(AutoIncrement = true)] 9 public int Id { get; set; } 10 11 [UIOMaticNameField] 12 public string Name { get; set; } 13 14 [UIOMaticField(Is castrated, Has the dog been castrated)] 15 public bool IsCastrated { get; set; } 16 17 18 [UIOMaticField(Owner, Select the owner of the dog, View = dropdown, 19 Config = {’typeName’: ‘Example.Models.Person, Example’, ‘valueColumn’: ‘Id’, ’sortColumn’: ‘FirstName’, ‘textTemplate’ : ‘FirstName + \” \”+ LastName ‘})] 20 [UIOMaticIgnoreFromListView] 21 public int OwnerId { get; set; } 22 23 public override string ToString() 24 { 25 return Name; 26 } 27 28 public IEnumerable<Exception> Validate() 29 { 30 var exs = new List<Exception>(); 31 32 if (string.IsNullOrEmpty(Name)) 33 exs.Add(new Exception(Please provide a value for name)); 34 35 if (OwnerId == 0) 36 exs.Add(new Exception(Please select an owner)); 37 38 39 return exs; 40 } 41 }

As you see the dog has an OwnerId property that is linked to the Person class  (in the way described here http://www.nibble.be/?p=491 ) So we get a nice dropdown where we can select a Person (instead of having to type in the id)

Person

1 [UIOMatic(People, icon-users, icon-user,SortColumn=FirstName)] 2 [TableName(People)] 3 public class Person : IUIOMaticModel 4 { 5 public Person() { } 6 7 [UIOMaticIgnoreField] 8 [PrimaryKeyColumn(AutoIncrement = true)] 9 public int Id { get; set; } 10 11 [UIOMaticField(Firstname,Enter your firstname)] 12 public string FirstName { get; set; } 13 14 [UIOMaticField(Lastname, Enter your lastname)] 15 public string LastName { get; set; } 16 17 [UIOMaticField(Picture, Please select a picture,View =file)] 18 public string Picture { get; set; } 19 20 [Ignore] 21 [UIOMaticField(Dogs, “”, View =list, 22 Config = {’typeName’: ‘Example.Models.Dog, Example’, ‘foreignKeyColumn’ : ‘OwnerId’, ‘canEdit’ : false})] 23 public IEnumerable<Dog> Dogs { get; set; } 24 25 public override string ToString() 26 { 27 return FirstName + + LastName; 28 } 29 30 public IEnumerable<Exception> Validate() 31 { 32 var exs = new List<Exception>(); 33 34 if (string.IsNullOrEmpty(FirstName)) 35 exs.Add(new Exception(Please provide a value for first name)); 36 37 if (string.IsNullOrEmpty(LastName)) 38 exs.Add(new Exception(Please provide a value for last name)); 39 40 41 return exs; 42 } 43 }

On the person class you’ll see a Dogs property (that is set to Ignore since it isn’t an actuall column) making use of the list field view type (more on that in the docs)

The result

We get a nice overview off all dogs associated with the selected person

Screen Shot 2016-02-19 at 13.54.05

1 Comment so far

  1. Robert Bullock on March 29th, 2016

    This is better but still not quite ideal. What I’ve been searching for is I suppose a many-to-many editor. For example, we have a table of doctors and languages. So my data structure includes a joining table.

    Doctors: Id, Name, etc.
    Languages: Id, Language
    Doc2Languages: Id, docId, langId

    So I’m trying to find a nice simple way to allow users to edit these relationships. Unless I’m missing something, UI-O-Matic isn’t quite there, but I could have, for example, simply a “Language to Docs” editor where they select a doctor and a language for each record. Basically, editing the joining table directly rather than editing in context.

Leave a Reply