Skip to content

Conversation

@brandonkelly
Copy link
Member

Description

This adds new --set, --to, and --if-empty options to all resave/* commands, which can be used to bulk-set a new value on resaved elements’ attributes and custom fields.

Examples:

# set `foo` to "bar" if it’s empty:
php craft resave/entries --set foo --to "=bar" --if-empty

# copy the value of `baz` to `foo`:
php craft resave/entries --set foo --to baz

# clear out the `foo` field/attribute:
php craft resave/entries --set foo --to :empty:

# set `foo` to the result of an object template (Twig allowed):
php craft resave/entries --set foo --to "={author.name}"

# set `foo` to the result of an arrow function:
php craft resave/entries --set foo --to "fn(\$entry) => \$entry->author->name"

Related issues

@brandonkelly brandonkelly merged commit edeaf1e into develop Jan 11, 2022
@brandonkelly brandonkelly deleted the feature/resave-set branch January 11, 2022 22:54
mattstein pushed a commit to craftcms/docs that referenced this pull request Jan 18, 2022
@samhibberd
Copy link

Nice one @brandonkelly, some really nice updates to the resave command.

One thought, with the new functionality to now update content / attribute values as a part of this, should the dateUpdated value not update in line with any content changes. I can understand why this was stopped when elements were just resaved as is, but with content explicitly being updated?

Apologies if i'm missing something obvious.

And if you currently needed to mark the elements as updated at that point would it be safe to set the dateUpdated attribute to now manually?

@brandonkelly
Copy link
Member Author

@samhibberd It was intentional to not change dateUpdated even if --set is used. In general I expect this is going to be used for things like setting default values for new fields (e.g. #8461) or migrating content from one field to another (e.g. the new Alternative Text fields in Craft 4).

@samhibberd
Copy link

Digging deeper into the craft I can see why you have decided that makes sense.

In a situation that you would need to update the dateUpdated is updating the value set in the elements table sufficient?

@brandonkelly
Copy link
Member Author

@samhibberd Yeah that’s the place to do it.

@Kashkin
Copy link

Kashkin commented Feb 28, 2022

@brandonkelly I'm attempting to use --set and --to to update an Entries field, and not having any luck. Is this a supported use case, and if so, what is the syntax? If not, what are the supported field types that can be updated using the CLI?

@brandonkelly
Copy link
Member Author

@Kashkin For that you would need to use the PHP arrow function syntax, because relational fields need to be set to an array of related element IDs.

php craft resave/entries --set foo --to "fn(\$entry) => [1, 2, 3]"

@Kashkin
Copy link

Kashkin commented Mar 1, 2022

Thanks for the tip! That makes sense in retrospect, but it would be great to add that example to the docs!

@mattstein
Copy link
Contributor

Example added @Kashkin, thank you! We’ll also get a link to that article from the Console Commands page in the docs.

@Kashkin
Copy link

Kashkin commented Sep 14, 2022

@mattstein Hi Matt! Thanks for the continuously improving documentation. I can't seem to figure out the syntax for --to when setting a lightswitch field. Any ideas?

@brandonkelly
Copy link
Member Author

@Kashkin You can use =1 or =0:

php craft resave/entries --set myLightswitchField --to "=1"

@Kashkin
Copy link

Kashkin commented Sep 15, 2022

Thanks @brandonkelly! I feel really silly for not noticing the = in all the documentation. 🤦

@brandonkelly
Copy link
Member Author

Hah, no worries :) It’s required if you want to pass a literal value, as opposed to reference another field/attribute.

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

Successfully merging this pull request may close these issues.

5 participants