How can get all Related entries from target Link

Vote:
 

Hi,

As i understand we only get related entries for an entry. But I want to do the opposite.

Can from Target i want get all Source related entries

For examle:

Product 1 have related entries product1(part1, part2, part3, par4)

Product 2 have related entries product1(part1, part2)

Product 3 have related entries product1(part3, part4)

Product 4 have related entries product1(part1, part4)

=> i want from part1 get all Product1, Product2, Product4

https://world.episerver.com/documentation/developer-guides/commerce/catalogs/catalog-content/Related-entries/

Regards,

T

#225051
Edited, Jul 03, 2020 11:43
Vote:
 

Hi Pham,

I had a similar use case for one of our projects, I went about creating a bidirectional association. I associate the part to the product (Ex: Part1 is associated to Product1) and I created another association between product and part (Ex: Product1 is associated to Part1). I also segmented the associations into 2 different association groups.

Below is a quick sample of what you can do (will need some refinement):

associations.Add(new Association
                    {
                        Group = new AssociationGroup
                        {
                            Name = "ProductAssociation",
                            Description = "The product associated to the part",
                            SortOrder = 100
                        },
                        SortOrder = 100,
                        Source = partRef,
                        Target = variant.GetParentProducts().FirstOrDefault(),
                        Type = new AssociationType
                        {
                            Id = AssociationType.DefaultTypeId,
                            Description = string.Empty
                        }
                    });

                    associations.Add(new Association
                    {
                        Group = new AssociationGroup
                        {
                            Name = "PartAssociation",
                            Description = "The part associated to the product",
                            SortOrder = 100
                        },
                        SortOrder = 100,
                        Source = variant.GetParentProducts().FirstOrDefault(),
                        Target = partRef,
                        Type = new AssociationType
                        {
                            Id = AssociationType.DefaultTypeId,
                            Description = string.Empty
                        }
                    });
_associationRepository.Value.UpdateAssociations(associations);

And below is how it would look like under realted entries:

  1. Adding a product as a related entry to a part:
  2. Adding a part as a related entry to a product:
#225061
Edited, Jul 05, 2020 16:42
Vote:
 

Hi Siddharth Gupta,

I also did same as you did before. But i wonder why episerver does not support api to get bidirectional association.

Thank for your suggestion,

Regards,

Thao Pham

#225162
Jul 07, 2020 10:36
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.