Save of updated related field

Posts   
 
    
Posts: 134
Joined: 04-Mar-2005
# Posted on: 14-Jun-2005 21:21:02   

I have a question about what will or should happen in the following case:

  • Two entites exist with a 1:1 relationship (customer and extraCustomer).
  • On the customer entity there is a "field on related field" which is not readonly
  • The relationship between customer and extraCustomer is optional, i.e. there may not be a related extraCustomer record for every customer.

  • I fetch a customer entity with the extraCustomer in the prefetch

  • In this case there is no related extraCustomer record
  • I update the field on related field (i.e. the extraCustomer field) via the customer entity.
  • I save the customer entity

Should the extraCustomer record be created on save of the customer entity or are there some extra steps that need to be taken?

confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Jun-2005 21:43:53   

When there's no extraCustomer object available when setting the field on related field, no object is created for you. So the value is lost and there's no extraCustomer entity created on save. You want that to happen in your situation?

You could try to add to the Customer's InitClassEmpty method's user code region to set the ExtraCustomer object always to an instance using this.ExtraCustomer = new ExtraCustomerEntity(); . Then, during prefetch, the customers which have an extra customer get that object (the new one is removed) and the ones which don't have an extracustomer will keep their new ones. Of course, when saving the customer objects, you'll get all those new ones as well, so either remove them prior to saving (via a validator) or use a different strategy (for example by overriding the FieldOnRelatedField property in a derived CustomerEntity class, and in there, you check if there's already an extraCustomer instance, if not, first create it and then call the base' method.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 19-Aug-2005 21:07:26   

Otis wrote:

You could try to add to the Customer's InitClassEmpty method's user code region to set the ExtraCustomer object always to an instance using this.ExtraCustomer = new ExtraCustomerEntity(); . Then, during prefetch, the customers which have an extra customer get that object (the new one is removed) and the ones which don't have an extracustomer will keep their new ones.

I don't think this approach is going to work. There's a 1:1 between customer and extraCustomer and on setting the customerId the extraCustomer is decoupled which means I'm in the same place I was. disappointed

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 20-Aug-2005 03:06:03   

If your new ExtraCustomerEntity is created using this.ExtraCustomer = new ExtraCustomerEntity(); then you should be able to save this customer and recursively save it's ExtraCustomerEntity with whatever customerid the current customer has. Is this not the behavior that you are experiencing?

Posts: 134
Joined: 04-Mar-2005
# Posted on: 20-Aug-2005 14:35:15   

bclubb wrote:

If your new ExtraCustomerEntity is created using this.ExtraCustomer = new ExtraCustomerEntity(); then you should be able to save this customer and recursively save it's ExtraCustomerEntity with whatever customerid the current customer has. Is this not the behavior that you are experiencing?

I'm not even getting to the save, or the fetch for that matter.

I'm doing dim customer As New CustomerEntity(custId)

As part of the New method the extraCustomer entity is set in InitClassEmpty (as Frans recommended) however immediately after that the customerId is set and doing that automatically decouples all 1:1 related entities.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 22-Aug-2005 10:27:17   

I see what you mean, the constructor first calls InitClassEmpty(), then sets the PK field.

If you're setting the PK field's value to perform a fetch, you could do: dim customer As New CustomerEntity() customer.Fields(CInt(CustomerFieldIndex.CustomerID)).ForcedCurrentValueWrite(custID)

and then perform the fetch. I don't see any other way to solve this, other than manually setting the referenced extraCustomer after the creation of the CustomerEntity: I can't change the order in the constructor.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 22-Aug-2005 18:02:11   

Otis wrote:

I see what you mean, the constructor first calls InitClassEmpty(), then sets the PK field.

If you're setting the PK field's value to perform a fetch, you could do: dim customer As New CustomerEntity() customer.Fields(CInt(CustomerFieldIndex.CustomerID)).ForcedCurrentValueWrite(custID)

and then perform the fetch. I don't see any other way to solve this, other than manually setting the referenced extraCustomer after the creation of the CustomerEntity: I can't change the order in the constructor.

I had come to a similar conclusion but I wasn't 100% sure. Since 90% of my calls to get my customer go through one function (GetUsingPK) this is a feasible option. Thanks for your assistance.