Digital Commerce Blog - Blackbit

Lossless transformation of data models in Pimcore

Written by | Apr 29, 2019 11:15:35 AM

Structuring product and customer data perfectly for the online shop, marketing channels and sales processes is no easy task. When creating a data model, numerous factors must be taken into account so that the data can ultimately be maintained efficiently and stored and processed with high performance.

The digital commerce framework Pimcore offers a wide range of options for perfectly customising a data model to your own company processes. When selecting the data types for the individual fields in classes, object modules and field collections, there are various ways to achieve your goal. However, as company strategy, product range structure etc. can change quickly, even a data model that is perfect for the moment must be regularly adapted. This is the only way to ensure that data maintenance remains meaningful, consistent and efficient.

However, when data types are changed, the data previously entered in the objects is usually lost. It is therefore often necessary to weigh up between time-consuming data maintenance or accepting a data model that is sub-optimal for the new requirements.

With the BB Ultima Import Bundle, this choice between two evils no longer exists: it is now possible to change data types without losing data!

How to edit Pimcore data types without data loss

When data is imported into Pimcore with the BB Ultima Import Bundle, the following three steps are performed:

  1. Import the raw data from a data source
  2. Editing the raw data
  3. Importing the data into the Pimcore data objects

The Pimcore objects themselves can also serve as the data source for the first step. To convert the data type of an object field, the first step is to import the data from the Pimcore objects. You can then adjust the data type of the object field (or the object block or the field collection). Of course, the data is also lost in this case for the time being. However, you can now import the modified object fields on the basis of the raw data.

Practical example: Converting number fields to quantity value fields

To illustrate this, we will show you how you can convert a number field into a quantity value field in an object module. In our example, technical data was initially saved without units, as all data was available in cm when the data model was introduced. In future, however, the system should allow the units to be customised during product data maintenance (e.g. length in mm, cm, m).

The initial situation is therefore a product class with an object module field. The associated object modules contain various number fields:

Now create an import that is to import data from the Pimcore product objects:

For the raw data fields, you can access all getter methods of the product class. In this case, you need the object path and the object key for the later identification of the objects and, of course, the object blocks that contain the data fields to be changed.

After the raw data import, the data looks like this:

Now change the object module definition (from number field to quantity value field):

You then assign the raw data fields to the object fields to enable the object import: using drag and drop, the raw data field path is assigned to the object fields for the object path and the field key is assigned to the object key:

Both fields are key fields in this case. The system searches for objects for which both the path and the key match the raw data value.

In order to enter the raw data in the new object module fields, the raw data must now be prepared in such a way that the import bundle understands it and can call the respective setter methods correctly.

A callback function can be stored in the field assignment for this purpose. This allows you to access the raw data and the existing object data. In the case of object modules with quantity value fields, such a callback function for the width field in the chair object module looks like this:

// Parse the JSON string containing the object brick data - the import bundle takes care of converting the return value of the brick getter method into JSON params.value = JSON.parse(params.value); // If object has no technical data, null returned if(typeof params.value.primarytechnicaldata === "undefined") return null; // if it has the object module "chair" if(typeof params.value.primarytechnicaldata.chair !== "undefined") { // read the value from the old number field and return it as an array to fill the Quantity Value field [value, unit] return [params.value.primarytechnicaldata.chair.width, 'cm']; }

In this example, the callback function is stored as JavaScript code. However, it is also possible to store PHP code. All Pimcore data types can be accessed via the callback functions, including relations, relations with metadata, field collections, etc.

The BB Ultima Import Bundle therefore allows you to adapt your data model to changing requirements and further Pimcore developments without data loss.

Any questions about the BB Ultima Import Bundle for Pimcore?

Do you need help with data model changes or imports from external sources? We will be happy to answer your questions and support you with the implementation.