How to update related entities

Posts   
 
    
Kensmith
User
Posts: 2
Joined: 09-Oct-2006
# Posted on: 10-Oct-2006 19:52:30   

I need to update a field in my company table based on the user table. I have my use entity, Company entity and my usersCompany entity. My question sis how do I update just a field in usersCompany via the users table. Thanks

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 11-Oct-2006 00:30:01   

Hi,

there are several ways to do that.

just a little clarification first: if you have a UserCompany intermediate table, then it means a user can theoretically be related several companies, right? Otherwise the best design would be a single relation between User and Company. Anyway, it does not change how to proceed.

First, as you may have noticed, you can access entity and entitycollection properties from inside your entities, corresponding to your entity's relations. You can use them to set the relations between your entities (will update the corresponding foreign keys automatically) and access the related entities' properties.

Now if you only fetch your user, the usercompany and company inner collection will be empty. One way to retrieve an entity together with related entities is to use prefetch paths (see the manual for detailed instruction and code). Once your done with editing the inner entities, simply save the main one and that will make the updates recursively.

Now you may also keep with your sole user and deal with its company afterwards (for ressource consideration for instance). So if you want to get the company(s) related to your user in a second fetch, you may use a filter to do so. Your user class should contain methods such as GetRelationInfoCompanyCollectionViaUserCompany which gives you the filter you need to fetch the right company. So you can fetch, update and save your company that way.

Now there is another way to the update without performing prio fetching (adpater scenario). With the same filter mentioned above, you may use the UpdateEntitiesDirectly method from your adapter on a new company with the fields updated as you wish and the user filter.

Cheers