Inhalte aufrufen

Profilbild

How to use SyncMapping service


5 Antworten zu diesem Thema

#1 CompleteCoders

CompleteCoders

    Advanced Member

  • Members
  • 69 Beiträge

Geschrieben: 09 October 2017 - 04:56

I could use some advice on the syncmapping table.  I am doing an product extension and trying to figure out what values should go in the "SourceKey" and "ContextName" fields.

 

I think the ContextName will be the fieldname I am trying to save, but not positive.

 

Thanks in advanced.


  • RidgeOi gefällt das

#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3308 Beiträge

Geschrieben: 09 October 2017 - 11:02

You can use these fields according to your requirements. ContextName is used to determine to whom the record belongs, e.g. the system name of a plugin. If you use SyncMapping for different tasks, plugins, working units etc. you can use it to be able to differentiate between the different data records.
 
SourceKey is an additional, optional field used for the mapping, especially when EntityId is not sufficient to ensure a correct mapping.

  • RidgeOi gefällt das

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#3 Murat Cakir

Murat Cakir

    SmartStore AG

  • Administrators
  • 1079 Beiträge

Geschrieben: 09 October 2017 - 17:34

For a better illustration... let's assume you want to import products from an ERP system called MyERP:

 

  • ContextName: MyERP (any name which identifies the external system, plugin, app or whatever)
  • EntityName: Product (makes sense if you deal with multiple entity types from the same source)
  • SourceKey: [ProductIDFromMyERP] (the external system's Id/Key for this product entity)
  • EntityId: [ProductIDInSmartStore] (the ID of this entity in the Product table) 

Murat Cakir
SmartStore AG


#4 CompleteCoders

CompleteCoders

    Advanced Member

  • Members
  • 69 Beiträge

Geschrieben: 10 October 2017 - 03:03

Thank you for the reply but I'm still a bit confused about one part.  

 

There is an index:

 

CREATE UNIQUE NONCLUSTERED INDEX [IX_SyncMapping_BySource] ON [dbo].[SyncMapping]
(
[SourceKey] ASC,
[EntityName] ASC,
[ContextName] ASC
)
 
If I setup according to your plan:
 
  • ContextName: MyPluginName
  • EntityName: Product
  • SourceKey: null (I am just saving extraneous data about the product and there is no link)
  • EntityID: ProductID

 

This would cause duplicates because there all products would have the same value for ContextName, EntityName, SourceKey.  It seems like I don't have any choice but to make SouceKey = ProductID.

 

And in my case I need to save multiple extra fields for one product so either I have to change EntityName to be the fieldname or I would get duplicates on just one product.  I could do SourceKey = ProductID + FieldName but then I would have to parse it out. 

 

Thoughts?

 

Thanks



#5 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3308 Beiträge

Geschrieben: 10 October 2017 - 10:44

"It seems like I don't have any choice but to make SouceKey = ProductID."
That's ok.
 
"And in my case I need to save multiple extra fields for one product..."
Create a new class with all fields, add Serializable attribute to it, Json serialize the class (per product) to a string and store it in SyncMapping.CustomString.

Schöne Grüße aus Düsseldorf,
Marcus Gesing


#6 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3308 Beiträge

Geschrieben: 24 September 2018 - 20:06

Just another example:

var syncMapping = _syncMappingService.GetSyncMappingByEntity(customer.Id, "Customer", "MyCompany.MyPluginName");
if (syncMapping != null)
{
	syncMapping.CustomString = JsonConvert.SerializeObject(my_model_instance);
	_syncMappingService.UpdateSyncMapping(syncMapping);
}
else
{
	syncMapping = new SyncMapping
	{
		EntityId = customer.Id,
		EntityName = "Customer",
		ContextName = "MyCompany.MyPluginName",
		SourceKey = yourExternalId,	//optional
		CustomString = JsonConvert.SerializeObject(my_model_instance)
	};
	_syncMappingService.InsertSyncMapping(syncMapping);
}

Schöne Grüße aus Düsseldorf,
Marcus Gesing




Auf dieses Thema antworten