Reputation: 25
I'm trying to import data objects from CVS's files and exporting data to XML files from Pimcore using php scripting. I've managed to import data from CVS files, I am however not able to figure out how to export to a XML file.
I've tried looking around the web and the official documentation doesn't say much(www.pimcore.org/wiki/display/PIMCORE/External+System+Interaction ):
Export of data objects can be achieved programmatically or through pimcore CSV export. The UI export can be found when clicking on an object folder and selecting the Search, Edit & Export Tab.
I've found various pieces of "loose code", like:
include("pimcore/cli/startup.php"); $class = Object_Class::getById(1); echo Object_Class_Service::generateClassDefinitionXml($class);
I've even managed to find the "Object_Class_Service" mentioned in the above example but to no avail, it simply goes out error whenever I try to use it, from what I can tell it doesn't even work as it should.
I've looked for various Helpers and other means to find the correct way to export pimcore data objects to XML but there seems to be literally nothing.
The export is quite simple, one data object, let's name it User, with a input field with the getter/setter "Name".
I need to create PHP script which exports the data object 'User' along with the data. How on earth do you achieve this by scripting?
Upvotes: 2
Views: 2661
Reputation: 1276
The best way to save the class definitions, is to use the JSON as that is what Pimcore uses for Class definition export and import.
/*
* Export class definition
*/
// !!! This is the id of the object class, not the id of object instance.
// Go to Settings > Object > Classes and find the object class ID there
$classId = 5;
$class = Pimcore\Model\Object\ClassDefinition::getById($classId);
$classDefinitionJson = Pimcore\Model\Object\ClassDefinition\Service::generateClassDefinitionJson($class);
header("Content-type: application/json");
header("Content-Disposition: attachment; filename=\"class_" . $class->getName() . "_export.json\"");
echo $classDefinitionJson;
/*
* Import class definition
*/
$nameOfTheClass = "someClass";
$class = Pimcore\Model\Object\ClassDefinition::getByName($name);
if (!$class) {
$class = new Pimcore\Model\Object\ClassDefinition();
$class->setName($name);
}
$classDefinitionJson = @file_get_contents($classDefinitionJson);
$classDefinitionJson = json_decode($classDefinitionJson, true);
Pimcore\Model\Object\ClassDefinition\Service::importClassDefinitionFromJson($class, $classDefinitionJson);
Please note that the Object Class Id and the name of the object class are not saved within the object definition file. That is why this is needed:
$nameOfTheClass = "someClass";
$class = Pimcore\Model\Object\ClassDefinition::getByName($name);
Upvotes: 2