Pimcore\Model\Object\Fieldcollection\Definition::save PHP Method

save() public method

public save ( )
    public function save()
    {
        if (!$this->getKey()) {
            throw new \Exception("A field-collection needs a key to be saved!");
        }
        $infoDocBlock = $this->getInfoDocBlock();
        $definitionFile = $this->getDefinitionFile();
        $clone = clone $this;
        $clone->setDao(null);
        unset($clone->fieldDefinitions);
        $exportedClass = var_export($clone, true);
        $data = '<?php ';
        $data .= "\n\n";
        $data .= $infoDocBlock;
        $data .= "\n\n";
        $data .= "\nreturn " . $exportedClass . ";\n";
        \Pimcore\File::put($definitionFile, $data);
        $extendClass = "Object\\Fieldcollection\\Data\\AbstractData";
        if ($this->getParentClass()) {
            $extendClass = $this->getParentClass();
            $extendClass = "\\" . ltrim($extendClass, "\\");
        }
        // create class file
        $cd = '<?php ';
        $cd .= "\n\n";
        $cd .= $infoDocBlock;
        $cd .= "\n\n";
        $cd .= "namespace Pimcore\\Model\\Object\\Fieldcollection\\Data;";
        $cd .= "\n\n";
        $cd .= "use Pimcore\\Model\\Object;";
        $cd .= "\n\n";
        $cd .= "class " . ucfirst($this->getKey()) . " extends " . $extendClass . "  {";
        $cd .= "\n\n";
        $cd .= 'public $type = "' . $this->getKey() . "\";\n";
        if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
            foreach ($this->getFieldDefinitions() as $key => $def) {
                $cd .= "public \$" . $key . ";\n";
            }
        }
        $cd .= "\n\n";
        if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
            $relationTypes = [];
            foreach ($this->getFieldDefinitions() as $key => $def) {
                /**
                 * @var $def Object\ClassDefinition\Data
                 */
                $cd .= $def->getGetterCodeFieldcollection($this);
                if ($def instanceof Object\ClassDefinition\Data\Localizedfields) {
                    foreach ($def->getFieldDefinitions() as $localizedFd) {
                        /**
                         * @var $fd Object\ClassDefinition\Data
                         */
                        $cd .= $localizedFd->getGetterCodeLocalizedfields($this);
                    }
                }
                $cd .= $def->getSetterCodeFieldcollection($this);
                if ($def instanceof Object\ClassDefinition\Data\Localizedfields) {
                    foreach ($def->getFieldDefinitions() as $localizedFd) {
                        /**
                         * @var $fd Object\ClassDefinition\Data
                         */
                        $cd .= $localizedFd->getSetterCodeLocalizedfields($this);
                    }
                }
            }
        }
        $cd .= "}\n";
        $cd .= "\n";
        File::put($this->getPhpClassFile(), $cd);
        // update classes
        $classList = new Object\ClassDefinition\Listing();
        $classes = $classList->load();
        if (is_array($classes)) {
            foreach ($classes as $class) {
                foreach ($class->getFieldDefinitions() as $fieldDef) {
                    if ($fieldDef instanceof Object\ClassDefinition\Data\Fieldcollections) {
                        if (in_array($this->getKey(), $fieldDef->getAllowedTypes())) {
                            $this->getDao()->createUpdateTable($class);
                            break;
                        }
                    }
                }
            }
        }
    }

Usage Example

Example #1
0
 public function fieldcollectionUpdateAction()
 {
     try {
         $key = $this->getParam("key");
         if ($this->getParam("task") == "add") {
             // check for existing fieldcollection with same name with different lower/upper cases
             $list = new Object\Fieldcollection\Definition\Listing();
             $list = $list->load();
             foreach ($list as $item) {
                 if (strtolower($key) === strtolower($item->getKey())) {
                     throw new \Exception("FieldCollection with the same name already exists (lower/upper cases may be different)");
                 }
             }
         }
         $fc = new Object\Fieldcollection\Definition();
         $fc->setKey($key);
         if ($this->getParam("values")) {
             $values = \Zend_Json::decode($this->getParam("values"));
             $fc->setParentClass($values["parentClass"]);
         }
         if ($this->getParam("configuration")) {
             $configuration = \Zend_Json::decode($this->getParam("configuration"));
             $configuration["datatype"] = "layout";
             $configuration["fieldtype"] = "panel";
             $layout = Object\ClassDefinition\Service::generateLayoutTreeFromArray($configuration, true);
             $fc->setLayoutDefinitions($layout);
         }
         $fc->save();
         $this->_helper->json(["success" => true, "id" => $fc->getKey()]);
     } catch (\Exception $e) {
         Logger::error($e->getMessage());
         $this->_helper->json(["success" => false, "message" => $e->getMessage()]);
     }
 }
All Usage Examples Of Pimcore\Model\Object\Fieldcollection\Definition::save