Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems while importing quantity values with the data import configuration tool #4487

Open
jacobsyentl opened this issue Jun 5, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@jacobsyentl
Copy link
Contributor

commented Jun 5, 2019

Expected behavior

Importing quantity values is as simple as importing a number.

Actual behavior

To import quantity values you must give a value+" "+the id of the unit.

Steps to reproduce

  1. Import a CSV file for an object with quantity values.
  2. Configure the column configuration.
  3. Start the import.
  4. Quantity value will not be set. (because id of unit was not given)

Code example (possible patch)

diff --git a/models/DataObject/ClassDefinition/Data/QuantityValue.php b/models/DataObject/ClassDefinition/Data/QuantityValue.php
index 0eaabe4..6213bd4 100644
--- a/models/DataObject/ClassDefinition/Data/QuantityValue.php
+++ b/models/DataObject/ClassDefinition/Data/QuantityValue.php
@@ -426,14 +426,43 @@ class QuantityValue extends Data implements ResourcePersistenceAwareInterface, Q
 
         $value = null;
         if ($values[0] && $values[1]) {
-            $number = (float) str_replace(',', '.', $values[0]);
-            $value = new  \Pimcore\Model\DataObject\Data\QuantityValue($number, $values[1]);
+            $value = $this->createNewQuantityValueObject($this->convertStringToFloat($values[0]),$values[1]);
+        }
+
+        if($values[0] && !isset($values[1])){
+            $value = $this->createNewQuantityValueObject($this->convertStringToFloat($values[0]));
         }
 
         return $value;
     }
 
     /**
+     * Convert any sort of value to float
+     *
+     * @param $value
+     * @return float
+     */
+    private function convertStringToFloat($value)
+    {
+        return (float) str_replace(',', '.', $value);
+    }
+
+    /**
+     * Function to create (and return) a new QuantityValue object
+     *
+     * @param $value
+     * @param int|null $unitId
+     * @return Model\DataObject\Data\QuantityValue
+     */
+    private function createNewQuantityValueObject($value,int $unitId = null)
+    {
+        if($unitId == null){
+            $unitId = $this->getDefaultUnit();
+        }
+        return new  \Pimcore\Model\DataObject\Data\QuantityValue($this->convertStringToFloat($value),$unitId);
+    }
+
+    /**
      * display the quantity value field data in the grid
      *
      * @param $data

Patch description

In case a unit is not found, the importer can fallback on the default unit for that attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.