Migration 在Craft CMS迁移中更新MatrixBlock“字段布局字段”的句柄

Migration 在Craft CMS迁移中更新MatrixBlock“字段布局字段”的句柄,migration,craftcms,Migration,Craftcms,读完这本书后,我兴奋不已。他们在导入10个左右在我们网站上工作的开发人员的相同更改时非常有用 当我试图通过迁移来更改矩阵块中块类型中各个字段的句柄时,我遇到了一个障碍。字段本身可以很容易地更新其句柄属性,但该矩阵块的内容matrixcontent_uuxxxxx的列表不会更新以反映任何更新的句柄。矩阵块及其关联的矩阵内容表之间的关联仅存在于该矩阵块的字段记录的info列中 如果矩阵块的字段是通过CMS更新的,那么变化会被反映出来,所以它一定是在Craft的源代码中的某个地方,但通过CMS它并不明

读完这本书后,我兴奋不已。他们在导入10个左右在我们网站上工作的开发人员的相同更改时非常有用

当我试图通过迁移来更改矩阵块中块类型中各个字段的句柄时,我遇到了一个障碍。字段本身可以很容易地更新其句柄属性,但该矩阵块的内容matrixcontent_uuxxxxx的列表不会更新以反映任何更新的句柄。矩阵块及其关联的矩阵内容表之间的关联仅存在于该矩阵块的字段记录的info列中

如果矩阵块的字段是通过CMS更新的,那么变化会被反映出来,所以它一定是在Craft的源代码中的某个地方,但通过CMS它并不明显

有什么想法吗

编辑以添加迁移代码段:

公共职能安全 { //标签等的字符串。 $matrix\u block\u指令=指令; $block_label=视频; $block_handle=视频; $field\u handle=视频\u url; $field\u label=视频URL; $field_instructions=添加YouTube或Facebook视频URL。; //获取条目本身上使用的MatrixBlock字段 $video\u gallery\u 1=Craft::$app->fields->getFieldByHandlehandle\u 1; $video_gallery_2=Craft::$app->fields->getFieldByHandlehandle_2; $video_gallery_3=Craft::$app->fields->getFieldByHandlehandle_3; $galleries=[$video_gallery_1、$video_gallery_2、$video_gallery_3]; foreach$gallery作为$gallery{ //正在获取此特定MatrixBlock字段的记录。 $gallery\u record=\craft\records\Field::find->where ['id'=>$gallery->id] ->一,; //正在获取此特定MatrixBlockType的记录 $gallery\u block\u id=$gallery->getBlockTypes[0]->id; $gallery\u block=\craft\records\MatrixBlockType::find->where ['id'=>$gallery\u block\u id] ->一,; //为MatrixBlockType指定标准标签 $gallery\u block->name=$block\u标签; $gallery\u block->handle=$block\u handle; $gallery_block->更新; //获取要编辑的特定…1…字段 $field\u group=\craft\records\FieldLayout::find->where ['id'=>$gallery\u block->fieldLayoutId] ->一,; $field\u layout\u field=$field\u group->fields[0]; $field=$field\u layout\u field->field; $field=\craft\records\field::find->where ['id'=>$field->id] ->一,; //为标签指定标准标签 $field->name=$field\u标签; $field->handle=$field\u handle; $field->instructions=$field\u指令; $field->update; //使用新指令更新MatrixBlock记录 $gallery_记录->刷新; $gallery\u record->instructions=$matrix\u block\u instructions; $gallery_记录->更新; }
好吧,如果有人想弄明白这一点,我很抱歉,但我上面的方法是一种疯子的方法,但我找到了自己的解决办法

这里的关键是,我应该与craft\fields\MatrixBlock和craft\fields\PlainText对象交互,而不是与craft\records\Field对象交互。在\craft\services\fields中有一个保存字段的方法,需要实现。这实际上是返回的默认类,我在c中为自己做了更多的工作颂歌

在该foreach循环中,结果如下:

//正在获取此特定MatrixBlock字段的记录。 $gallery->instructions=$matrix\u block\u instructions; //更新MatrixBlockType $gallery\u block\u id=$gallery->getBlockTypes[0]->id; $gallery\u block=\craft\records\MatrixBlockType::find->where ['id'=>$gallery\u block\u id] ->一,; $gallery\u block->name=$block\u标签; $gallery\u block->handle=$block\u handle; $gallery_block->更新; //更新实际字段。 $field=$gallery->blockTypeFields[0]; $field->name=$field\u标签; $field->handle=$field\u handle; $field->instructions=$field\u指令; 工艺::$app->getFields->saveField$field; 工艺::$app->getFields->saveField$gallery;
谢谢你看了这篇文章,也很抱歉你是个疯子。

你能发布你的迁移代码吗?@BrandonKelly谢谢你的快速回复!补充到原来的问题中。很高兴你明白了!我已经开始回复了,但后来被午餐打断了: