I'm trying to update a specific field (a dropdown) in an existing block (of type 'subhoofding') in an existing matrix field 'contentBuilder' The migration itself runs fine, show the new value as output, but some bizarre reason it gets not saved to the database.
public function safeUp()
{
$sites = Craft::$app->sites->getAllSites();
foreach($sites as $site) {
//craft::dd($site->id);
$contact = Entry::find()->slug('contact')->siteId($site->id)->one();
// Read content from matrix field
$blocks = $contact->getFieldValue('contentBuilder')->all();
foreach($blocks as $block) {
//craft::dd($block->type);
if ($block->type == "subhoofding") {
echo("old value = " . $block->getFieldValue('alignering') . "\n");
$block->setFieldValue('alignering', 'center');
echo("new value = " . $block->getFieldValue('alignering') . "\n");
}
}
// And save the entry contact
$success = \Craft::$app->elements->saveElement($contact);
if ($success) {
echo("SUCCESS \n");
}
echo (" --- on to next site ---- \n");
}
return true;