Search relation problem

Posts   
 
    
Posts: 5
Joined: 13-Oct-2010
# Posted on: 13-Oct-2010 11:29:50   

I have a problem generation te right search query. I have a customer table, linking to cunstomerContacts, who links to contact. Customer also links to customershops, linking to shops, to shopscontacts and also to Contact. So shop uses the contact entity and customer uses the same contact entity. I want to search for the contact name of the shop and the customer. I use te following code:

        var search = new RelationPredicateBucket(PPW.Common.Search.FieldsContainsValues(BtnEditSearch.Text, CustomerFields.Name1, ContactFields.Firstname, ShopFields.Name1));
        search.Relations.Add(CustomerEntity.Relations.CustomerContactEntityUsingCustomerId);
        search.Relations.Add(ContactEntity.Relations.CustomerContactEntityUsingContactId);
        search.Relations.Add(CustomerEntity.Relations.CustomerShopEntityUsingCustomerId);
        search.Relations.Add(ShopEntity.Relations.CustomerShopEntityUsingShopId);
        search.Relations.Add(ShopEntity.Relations.ShopContactEntityUsingShopId);
        search.Relations.Add(ContactEntity.Relations.ShopContactEntityUsingContactId);

The contactentity of the customercontact entity is joined when i look to the query, but the contactentity of the shopcontactentity is not.

Many thanks in advance for the help!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Oct-2010 16:56:17   

First thing: it's recommended to add the relations one by one in order as follows.

            search.Relations.Add(CustomerEntity.Relations.CustomerContactEntityUsingCustomerId);
            search.Relations.Add(CustomerContactEntity.Relations.ContactEntity...);
            search.Relations.Add(CustomerEntity.Relations.CustomerShopEntityUsingCustomerId);
            search.Relations.Add(CustomerShopEntity.Relations.ShopEntity...);
            search.Relations.Add(ShopEntity.Relations.ShopContactEntityUsingShopId);
            search.Relations.Add(ShopContactEntity.Relations.ContactEntity..., "Sc");

And since you add the relation (Join) to Contact more than once, then you should use aliases to differentiate between each Joined instance.

Then when filtering on fields from the contact table, if that field belongs to the aliased instance, then set the instance of the field being filtered... using specifiedEntityField.SetObjectAlias() method Just like writing SQL queries.