/**
  * @param array $fields
  * @param int   $grouping
  * @param $entity
  *
  * @throws FieldConfigurationException
  */
 public function addFromArray(array $fields, $grouping = 0, $entity = null)
 {
     $collection = new FieldCollection([], $this->em);
     $collection->setGrouping($grouping);
     foreach ($fields as $name => $value) {
         $storageTypeHandler = $this->getFieldType($name);
         $field = new FieldValue();
         $field->setName($this->getName());
         $dbHandler = $storageTypeHandler->getStorageType();
         // We'd prefer data set from an array to already be correctly hydrated, but as a helper we here
         // pass it through the hydrate step if the value is a string. This will take care of cases where
         // a date/datetime is passed as string rather than an object.
         if (is_string($value)) {
             $field->setValue($dbHandler->convertToPHPValue($value, $storageTypeHandler->getPlatform()));
         } else {
             $field->setValue($value);
         }
         $field->setFieldname($name);
         if ($entity) {
             $field->setContenttype((string) $entity->contenttype);
         }
         $field->setFieldtype($this->getFieldTypeName($field->getFieldname()));
         $field->setGrouping($grouping);
         $collection->add($field);
     }
     $this->add($collection);
 }
Example #2
0
 /**
  * @param array $fields
  * @param int   $grouping
  */
 public function addFromArray(array $fields, $grouping = 0)
 {
     $collection = new FieldCollection([], $this->em);
     $collection->setGrouping($grouping);
     foreach ($fields as $name => $value) {
         $field = new FieldValue();
         $field->setName($this->getName());
         $field->setValue($value);
         $field->setFieldname($name);
         $field->setGrouping($grouping);
         $collection->add($field);
     }
     $this->add($collection);
 }