how to add relation ship in Kentico(one to Many)

MohammadJavad Tavakoli asked on June 8, 2020 11:04

Dear Sir/Madam, I have a question about a little scenario that I noted it below. In this we scenario we have two tables Table A and Table B. Table A related by Table B with a Key(ex. ID).

For Example: Table A has some columns such as ProductId , ProductName , ProudactImage Table B has some columns such as FeatureId ,FeatureName , ProductId My Question Is : How to relation ship (one-to-Many) Between TableA And TableB in Kentico Admin?

Best Regards,

Recent Answers


Dmitry Bastron answered on June 8, 2020 11:14

Hi MohammadJavad,

When you mention "tables" - what Kentico entities do you mean? It could be custom table, module custom class, form or page type. All work slightly different. But in the documentation you can find the way how to store one-to-many relationships for module custom classes in this article, and many-to-many in this article.

2 votesVote for this answer Mark as a Correct answer

MohammadJavad Tavakoli answered on June 8, 2020 11:56

thanks for the Answer

what I mean is the table when we define the page type page type Product (table A) , Page type Feature(table B) I want to link the product to the feature(Table B) using the ProductID

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on June 8, 2020 12:34

Ok, for page types you can use page relationships (if you need both accessing products from feature and features from product) or pages field data type which is based on relationships under the hood (this will work well if you only need getting features for a product). The only drawback of using relationships for your case is that relationships are not extendable, i.e. you cannot introduce your custom fields into relationship. So that the relationship will store in your case only ProductID and FeatureID.

Other option would be creating your own custom class to store this relation but it will require quite a lot of custom steps to do:

  • create module custom class to store relationships with your required custom fields
  • create a form control to select related features and products in admin
  • write some code to retrieve related features and products
1 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on June 8, 2020 19:46

I would recommend my Relationships Extended module as it allows you to create interfaces to do these custom mapping.

https://github.com/KenticoDevTrev/RelationshipsExtended

https://github.com/KenticoDevTrev/RelationshipsExtended/wiki/Relationships-Extended-Overview

With this you can configure a "Related Table B" UI element and point it to your table, set your relationship fields (ProductID to ProductID), etc.

1 votesVote for this answer Mark as a Correct answer

MohammadJavad Tavakoli answered on June 9, 2020 12:55

Thank you dear Dmitry. I've done noted steps; At this time I don't know how can I write code to get that child fields. As a matter of fact, like Entity Frame work in .Net (Navigation Property) for example Product.Feature.featureName How can I retrieve their fields in kentico model after noted steps by code?

0 votesVote for this answer Mark as a Correct answer

MohammadJavad Tavakoli answered on June 9, 2020 12:57

Dear Trevor, Thanks for your help. How can I use it in my code ? Do you have a sample about it to use RelationshipsExtended ; after I Installed it in my projects? Thanks a lot.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on June 9, 2020 13:48

Read the documentation I linked and it provides samples.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.