Bolt\Storage\Field\Collection\RepeatingFieldCollection::addFromArray PHP Метод

addFromArray() публичный Метод

public addFromArray ( array $fields, integer $grouping, object $entity = null )
$fields array
$grouping integer
$entity object
    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);
    }

Usage Example

Пример #1
0
 private function repeaterHydrate($record, $app)
 {
     $contentTypeName = $record->contenttype['slug'];
     $contentType = $app['config']->get('contenttypes/' . $contentTypeName);
     $values = $this->localeValues;
     $localeSlug = $app['translate.slug'];
     if (isset($values[$localeSlug . 'data'])) {
         $localeData = json_decode($values[$localeSlug . 'data'], true);
         foreach ($localeData as $key => $value) {
             if ($key === 'templatefields') {
                 $templateFields = $app['config']->get('theme/templatefields/' . $record['template'] . '/fields');
                 foreach ($templateFields as $key => $field) {
                     if ($field['type'] === 'repeater') {
                         $localeData = json_decode($value[$key], true);
                         $originalMapping = null;
                         $originalMapping[$key]['fields'] = $templateFields[$key]['fields'];
                         $originalMapping[$key]['type'] = 'repeater';
                         $mapping = $app['storage.metadata']->getRepeaterMapping($originalMapping);
                         $repeater = new RepeatingFieldCollection($app['storage'], $mapping);
                         $repeater->setName($key);
                         foreach ($localeData as $subValue) {
                             $repeater->addFromArray($subValue);
                         }
                         $record['templatefields'][$key] = $repeater;
                     }
                 }
             }
             if (isset($contentType['fields'][$key]) && $contentType['fields'][$key]['type'] === 'repeater') {
                 /**
                  * Hackish fix until #5533 gets fixed, after that the
                  * following four (4) lines can be replaced with
                  * "$record[$key]->clear();"
                  */
                 $originalMapping[$key]['fields'] = $contentType['fields'][$key]['fields'];
                 $originalMapping[$key]['type'] = 'repeater';
                 $mapping = $app['storage.metadata']->getRepeaterMapping($originalMapping);
                 $record[$key] = new RepeatingFieldCollection($app['storage'], $mapping);
                 foreach ($value as $subValue) {
                     $record[$key]->addFromArray($subValue);
                 }
             }
         }
     }
 }
All Usage Examples Of Bolt\Storage\Field\Collection\RepeatingFieldCollection::addFromArray