Code Monkey home page Code Monkey logo

Comments (4)

ttempleton avatar ttempleton commented on July 17, 2024

As well as updating the elements_sites table, you will also need to change the site ID for the relevant Neo block structure(s). Since you're running Craft 3, this will involve updating the ownerSiteId value for the relevant rows in the neoblockstructures table (on Craft 4 and later, the column is just called siteId).

Both of these could be achieved using code based on the following as part of your event handling code:

$blockIds = \benf\neo\elements\Block::find()
  ->ownerId($element->id)
  ->siteId(11)
  ->ids();

$this->update(
  '{{%elements_sites}}',
  [
    'siteId' => 53,
  ],
  [
    'siteId' => 11,
    'elementId' => $blockIds,
  ]
);

$this->update(
  '{{%neoblockstructures}}',
  [
    'ownerSiteId' => 53,
  ],
  [
    'ownerSiteId' => 11,
    'ownerId' => $element->id,
  ]
);

Please let me know if you have any issues while making these changes.

from craft-neo.

hiasl avatar hiasl commented on July 17, 2024

Thanks for your answer @ttempleton !

1.) I tried it, both table rows in elements_sites and neoblockstructures are updated in the way I would expected it, but still the Neo blocks do not appear when I edit the owner entry in the CMS. The Neo field does not contain any blocks in edit mode.
2.) And because I would like to understand it: Why does moving the blocks work with Neo if I resave via Craft's console command? Isn't this the same as the resave service I use in the migration?

Below is the adapted content migration with your code, slightly modified with $this / $thisClone:

    public function safeUp()
    {
        $elementsService = Craft::$app->getElements();
        $entryQuery = Entry::find()->siteId(11)->id(118464); //
        $thisClone = $this;

        $elementsService->on(
            \craft\services\Elements::EVENT_BEFORE_RESAVE_ELEMENT,
            static function(\craft\events\BatchElementActionEvent $e) use ($entryQuery, $thisClone) {
                if ($e->query === $entryQuery) {
                    $element = $e->element;
                    $element->siteId = 53;
                    $element->sectionId = 36;

                    $blockIds = \benf\neo\elements\Block::find()
                        ->ownerId($element->id)
                        ->siteId(11)
                        ->ids();

                    $thisClone->update(
                        '{{%elements_sites}}',
                        [
                            'siteId' => 53,
                        ],
                        [
                            'siteId' => 11,
                            'elementId' => $blockIds,
                        ]
                    );

                    $thisClone->update(
                        '{{%neoblockstructures}}',
                        [
                            'ownerSiteId' => 53,
                        ],
                        [
                            'ownerSiteId' => 11,
                            'ownerId' => $element->id,
                        ]
                    );
                }
            }
        );

        $elementsService->resaveElements(
            $entryQuery,
            true,
            true
        );
    }

There must be something else which needs to be updated. How does the resave console command make it work?
Our Neo blocks are nested, does this cause any additional problems?

Thanks!

from craft-neo.

ttempleton avatar ttempleton commented on July 17, 2024

Whoops, I forgot about needing to update the content table as well:

$thisClone->update(
  '{{%content}}',
  [
    'siteId' => 53,
  ],
  [
    'siteId' => 11,
    'elementId' => $blockIds,
  ]
);

Regarding the resave/entries console command - it does also internally use $elementsService->resaveElements(), but it did not move over any Neo blocks in my testing (or any Matrix blocks, for that matter), so I'm not sure why it worked in your case.

from craft-neo.

hiasl avatar hiasl commented on July 17, 2024

Thanks again, this is working now. We also needed to move Matrix in NEO fields as well, but since we had your code, that was easy.

Btw: the resave/entries console command does move NEO blocks and Matrix as well, this would have been our fallback. But since it needs to be done manually, we prefer the migration.

from craft-neo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.