Skip to content

Instantly share code, notes, and snippets.

@dvesh3
Last active January 16, 2024 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dvesh3/50a1a99fd337d461e1652f2fd3b4d6cd to your computer and use it in GitHub Desktop.
Save dvesh3/50a1a99fd337d461e1652f2fd3b4d6cd to your computer and use it in GitHub Desktop.
Scripts for re-saving versions & recycle-bin items for the purpose of migrating o_ prefix properties on Pimcore 10.6
<?php
// file should be located in project root
use Pimcore\Tool\Serialize;
use Pimcore\Tool\Storage;
ini_set('max_execution_time', 0);
ini_set("memory_limit", "-1");
include_once __DIR__ . "/vendor/autoload.php";
$bootstrap = new \Pimcore\Bootstrap();
$bootstrap->startup();
$offset = 0;
$limit = 100;
$storage = Storage::get('recycle_bin');
while (true) {
$list = new \Pimcore\Model\Element\Recyclebin\Item\Listing();
$list->setOffset($offset);
$list->setLimit($limit);
$list->setCondition("`type` = 'object'");
$list = $list->load();
if (!$list) {
break;
}
foreach ($list as $item) {
$filePath = $item->getStoreageFile();
$data = $storage->read($item->getStoreageFile());
if (!$data) {
echo sprintf("RecycleBin (ID %d): cannot read recycled data.\n ", $item->getId());
continue;
}
$data = Serialize::unserialize($data);
if ($data instanceof Pimcore\Model\DataObject\AbstractObject) {
echo "Migrating recycled item: " . $item->getId() . "\n";
$item->setElement($data);
$item->save();
}
}
$offset += $limit;
Pimcore::collectGarbage();
}
echo "Done Recyclebin migration.";
<?php
// file should be located in project root
ini_set('max_execution_time', 0);
ini_set("memory_limit", "-1");
include_once __DIR__ . "/vendor/autoload.php";
$bootstrap = new \Pimcore\Bootstrap();
$bootstrap->startup();
$offset = 0;
$limit = 100;
while (true) {
$list = new \Pimcore\Model\Version\Listing();
$list->setOffset($offset);
$list->setLimit($limit);
$list->setCondition("`ctype` = 'object'");
$list = $list->load();
if (!$list) {
break;
}
foreach ($list as $version) {
$data = null;
$data = $version->getData();
if (!$data) {
echo sprintf("Version (ID %d): cannot read version data.\n ", $version->getId());
continue;
}
if ($data instanceof Pimcore\Model\DataObject\AbstractObject) {
echo "Migrating version ID: " . $version->getId() . "\n";
$version->setData($data);
$version->save();
}
}
$offset += $limit;
Pimcore::collectGarbage();
}
echo "Done Versions migration.";
@Fabian-Hofer-Mastertent

Thank you for the scripts!

In the pimcore10.6_migrate_o_prefixes_versions.php are two errors in line 29.
\n only works with double quotes, otherwise it will be printed as a normal string.
after the % you need to add a "d" otherwise you will get an error because sprintf can't know what type to output.

@storbahn
Copy link

In [pimcore10.6_migrate_o_prefixes_recyclebin.php]: the increasement of the offset should be after the foreach loop like in versions.

@dvesh3
Copy link
Author

dvesh3 commented Oct 30, 2023

@Fabian-Hofer-Mastertent @storbahn thanks for the review and suggestions. I applied the changes to the scripts.

@turbo-ele
Copy link

I also had to set the _objectAwareFields for all my versions containing localizedfields, as it otherwise threw an exception:
Uncaught PHP Exception Error: "Call to a member function getClass() on null" at /vendor/pimcore/pimcore/models/DataObject/Localizedfield.php

Here's my adaption

        if ($data instanceof Pimcore\Model\DataObject\AbstractObject) {
            echo "Migrating version ID: " . $version->getId() . "\n";

            // Without this localized fields are not loaded
            $data->setObjectVar('__objectAwareFields', ['localizedfields' => 1]);

            // Set the object on the localized fields if they were not set
            if (method_exists($data, 'getLocalizedfields')
                && $data->getLocalizedfields()
                && !$data->getLocalizedfields()->getObject()
            ) {
                $data->getLocalizedfields()->setObject($data);
            }

            $version->setData($data);
            $version->save();
        }

@Daniel-Ateles
Copy link

Have anyone made similar script for PricingRule-objects in the ecom-framework backed by the db table ecommerceframework_pricing_rule?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment