0

I've written a migration which successfully moves entries to a new section. The problem is that if you delete the old empty section, the moved entries are removed with it.

Before Migration:

Section 1 > EntryX

Section 2

After migration

Section 1 >

Section 2 > EntryX

Delete Section 1 results in EntryX being deleted.

What am I missing:

<?php

namespace craft\contentmigrations;

use Craft;
use craft\db\Migration;
use craft\elements\Entry;


/**
 * m231105_184930_resourcesLBMHToGeneral migration.
 */
class m231105_184930_resourcesLBMHToGeneral extends Migration
{
    
    public function moveEntriesDb() {

        // Moving entries from section
        $OLD_SECTION_ID = 31;

        // Moving entries to section
        $NEW_SECTION_ID = 54;

        // Change entry types
        $OLD_ENTRY_TYPE = 63;

        // Change entry types
        $NEW_ENTRY_TYPE = 101;


        // $industry[] = 331869;
        // $entries = Entry::find()->section('resourcesLbmh')->all();


        $lbhm = Entry::find()->section('industries')->id(331869)->one();
        $lbhmProdIds = $lbhm->relatedProducts->status(['live', 'pending', 'expired','disabled'])->ids();
        $buisnessApp = Entry::find()->section('industries')->id(717402)->one();
        $buisnessAppProdIds = $buisnessApp->relatedProducts->status(['live', 'pending', 'expired','disabled'])->ids();

        $industryArray = [$lbhm->id];

        $entries = Entry::find()->section('resourcesLbmh')->site('*')->status(['live', 'pending', 'expired','disabled']);

        echo "Saving entries with no relatedProducts set..." . PHP_EOL;

        foreach($entries->relatedProducts(':empty:')->all() as $entry) {

            // Update Entry
            $entry->setFieldValues(['relatedIndustries' => $industryArray]);
            $success = \Craft::$app->getElements()->saveElement($entry);

            if ($success) {
                echo 'Set entry ' . $entry . ' relatedIndustries to ' . implode(',', $industryArray) . PHP_EOL;
            } else {
                Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
            }
        }
        
        echo "Saving entries with related products" . PHP_EOL;

        foreach($entries->relatedProducts(':notempty:')->all() as $entry) {
            $industryArray = [];
            $relatedProductIds = [];

            foreach ($entry->relatedProducts as $product) {
                $relatedProductIds[] = $product->id;
            }

            if (count(array_intersect($relatedProductIds, $lbhmProdIds)) > 0) {
                $industryArray[] = $lbhm->id;
            }

            if (count(array_intersect($relatedProductIds, $buisnessAppProdIds)) > 0) {
                $industryArray[] = $buisnessApp->id;
            }

            // Update Entry
            $entry->setFieldValues(['relatedIndustries' => $industryArray]);
            $success = \Craft::$app->getElements()->saveElement($entry);

            if ($success) {
                echo 'Set entry ' . $entry . ' relatedIndustries to ' . implode(',', $industryArray) . PHP_EOL;
            } else {
                Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
            }     
        }

        $this->update('entries', ['sectionId' => $NEW_SECTION_ID], 'sectionId = ' . $OLD_SECTION_ID);
        $this->update('entryversions',['sectionId' => $NEW_SECTION_ID],'sectionId = ' . $OLD_SECTION_ID);
        $this->update('entries',['typeId' => $OLD_ENTRY_TYPE],'typeId = ' . $NEW_ENTRY_TYPE);

        echo '------------------------------------------------------' . PHP_EOL;
        echo 'Migrated entries from section:' . $OLD_SECTION_ID . ' to '. $NEW_SECTION_ID . PHP_EOL;

    }

    /**
     * @inheritdoc
     */
    public function safeUp(): bool
    {
        $this->moveEntriesDb();
        echo 'Finished!' . PHP_EOL;
        return true;
    }

    /**
     * @inheritdoc
     */
    public function safeDown(): bool
    {
        echo "m231105_184930_resourcesLBMHToGeneral cannot be reverted.\n";
        return false;
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.