Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Categories not updating when resaving entries view CLI #4672

Closed
shornuk opened this issue Jul 30, 2019 · 8 comments
Closed

Categories not updating when resaving entries view CLI #4672

shornuk opened this issue Jul 30, 2019 · 8 comments

Comments

@shornuk
Copy link

shornuk commented Jul 30, 2019

Description

Have a strange issue. I had a category group which consisted of a mix of 1 and 2 level categories. I updated the categories so that every category had a parent and it's a max of two levels.

When viewing an entry with a categories field for this group, if the original category was a (previously) single level category, it would just show that one. When I resave on the entry it updates to show the new parent for that category.

When I run a resave/entries from the cli it doesn't update these and the entry still doesn't recognise the parent category. (Query for the parent on the front end shows null until the entry is directly saved.)

Additional info

  • Craft version: 3.2.7
  • PHP version: 7.1
@brandonkelly
Copy link
Member

Categories fields only self-correct their selected relations when their field value changes, which is why resave/entries isn’t working for you (no new field values are set on the entries during a resave), whereas when you save the entry in the Control Panel, all field values are resubmitted to the entry.

This shouldn’t have been a problem for you to begin with though. When you move a category under another one, there’s code in place to ensure that every Categories field that relates the moved category will also relate its new ancestors.

I did notice that it wasn’t working as expected when the new ancestors were disabled, though, and I’ve fixed that for the next release. Could that have been the case here?

@shornuk
Copy link
Author

shornuk commented Jul 30, 2019

Ah OK. That's why I was confused. None of the cats were disabled so I was assuming that they should automatically update.
So is there anyway to force the entries to update the categories via a resave? I've got 1000+ entries that need updating and obviously can't go through manually. Would changing the slugs of all the categories force them to update via the resave command?

@brandonkelly
Copy link
Member

brandonkelly commented Jul 30, 2019

None of the cats were disabled so I was assuming that they should automatically update.

Hm, in that case I’m not able to reproduce. How did you go about changing the category hierarchy? Just from the Control Panel UI, or did you do it programmatically?

So is there anyway to force the entries to update the categories via a resave?

Yeah, you could write a script that does it. Since this is just a one-time thing, you could copy your web/index.php file as web/resave-entries.php, and delete this line from it:

$app->run();

Then add this to the end:

$section = 'yourSectionHandle';
$entryType = 'yourEntryTypeHandle';
$catField = 'yourCategoriesFieldHandle';

$query = craft\elements\Entry::find()
    ->section($section)
    ->type($entryType)
    ->siteId('*')
    ->unique();

foreach ($query->each() as $entry) {
    /** @var craft\elements\Entry $entry */
    $entry->setFieldValue($catField, $entry->$catField->anyStatus()->ids());
    $entry->setScenario(craft\base\Element::SCENARIO_ESSENTIALS);
    $entry->resaving = true;
    $app->elements->saveElement($entry);
}

echo 'Done resaving entries';

Replace yourSectionHandle, yourEntryTypeHandle, and yourCategoriesFieldHandle with the appropriate values. And if your Categories field is set to manage relations on a per-site basis, then you will also need to remove the ->unique() param.

Then access http://yoursite.com/resave-entries.php in your browser, and the script will go to work. Delete the script once it’s done.

@shornuk
Copy link
Author

shornuk commented Jul 30, 2019

Awesome. I'll give it a try this evening. For info, the categories hierarchy was done via the CP.

@brandonkelly
Copy link
Member

Can you check if you are able to reliably reproduce the original issue – that new ancestors are not automatically added to existing relations? If so, can you please send a database backup and your composer.json and composer.lock files over to [email protected]?

@shornuk
Copy link
Author

shornuk commented Jul 30, 2019

Will do.

@shornuk
Copy link
Author

shornuk commented Jul 30, 2019

Just had a changes to look at this. I've moved the categories around in the category group (just a simple drag reorder) and once I do that the entries are now showing the correct branches. Seems like this fixes this issue as there's only 20-30 odd categories so not too bad to shuffle them about.

On a side note, and possibly (probably) not related, when I do a resave/entries i get queued task for updating the search index like this...
Screen Shot 2019-07-30 at 20 02 38
Running two at a time. I though that used to show as a single task? Occasionally when trying to view the list from the CP it causes the site to freeze.

@brandonkelly
Copy link
Member

It is possible to have multiple job workers, especially if you are using the CLI to create them, but I suppose a race condition could occur where multiple could be started by the browser as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants