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;
}
}
}
}
}
}