在阅读了这篇优秀的Medium文章后,我对CraftCMS中的迁移感到兴奋。它们在为我们网站上工作的 10 名左右开发人员导入相同的更改方面非常有用。
当尝试通过迁移更改矩阵块(whew(内块类型中各个字段的句柄时,我遇到了一个障碍。字段本身可以很容易地更新其"handle"属性,但该矩阵块的内容(matrixcontent_xxxxx
(的列表不会更新以反映任何更新的句柄。矩阵块与其关联的矩阵内容表之间的关联仅存在于该矩阵块的field
记录的info
列中。
如果矩阵块的字段通过CMS更新,则更改会反映出来,因此它必须位于Craft源代码中的某个地方,但通过Craft CMS类参考并不明显。
有什么想法吗?
已编辑以添加迁移代码段:
public function safeUp()
{
// Strings for labels, etc.
$matrix_block_instructions = "instructions";
$block_label = "Video";
$block_handle = "video";
$field_handle = "video_url";
$field_label = "Video URL";
$field_instructions = "Add a YouTube or Facebook video URL.";
// Fetching the MatrixBlock fields used on the entries themselves
$video_gallery_1 = Craft::$app->fields->getFieldByHandle("handle_1");
$video_gallery_2 = Craft::$app->fields->getFieldByHandle("handle_2");
$video_gallery_3 = Craft::$app->fields->getFieldByHandle("handle_3");
$galleries = [$video_gallery_1, $video_gallery_2, $video_gallery_3];
foreach( $galleries as $gallery ) {
// Fetching the record for this specific MatrixBlock field.
$gallery_record = craftrecordsField::find()->where(
['id' => $gallery->id]
)->one();
// Fetching the record for this specific MatrixBlockType
$gallery_block_id = $gallery->getBlockTypes()[0]->id;
$gallery_block = craftrecordsMatrixBlockType::find()->where(
['id' => $gallery_block_id]
)->one();
// Assigning standard labels for the MatrixBlockType
$gallery_block->name = $block_label;
$gallery_block->handle = $block_handle;
$gallery_block->update();
// Getting the specific ... 1 ... field to edit
$field_group = craftrecordsFieldLayout::find()->where(
['id' => $gallery_block->fieldLayoutId]
)->one();
$field_layout_field = $field_group->fields[0];
$field = $field_layout_field->field;
$field = craftrecordsField::find()->where(
['id' => $field->id]
)->one();
// Assigning standard labels for the Label
$field->name = $field_label;
$field->handle = $field_handle;
$field->instructions = $field_instructions;
$field->update();
// Updating the MatrixBlock record with new instructions
$gallery_record->refresh();
$gallery_record->instructions = $matrix_block_instructions;
$gallery_record->update();
}
,所以如果有人想弄清楚这一点,我深表歉意,但我上面的方法有点像疯子的方法,但我已经找到了自己的解决方案。
这里的关键是我应该与craftfieldsMatrixBlock
和craftfieldsPlainText
对象进行交互,而不是craftrecordsField
对象。craftservicesFields
中有一个方法用于保存需要实现字段接口的字段。这实际上是返回的默认类,我在代码中为自己做了更多的工作!
在该foreach
循环中,这解决了:
// Fetching the record for this specific MatrixBlock field.
$gallery->instructions = $matrix_block_instructions;
// Update the MatrixBlockType
$gallery_block_id = $gallery->getBlockTypes()[0]->id;
$gallery_block = craftrecordsMatrixBlockType::find()->where(
['id' => $gallery_block_id]
)->one();
$gallery_block->name = $block_label;
$gallery_block->handle = $block_handle;
$gallery_block->update();
// Update the actual field.
$field = $gallery->blockTypeFields[0];
$field->name = $field_label;
$field->handle = $field_handle;
$field->instructions = $field_instructions;
Craft::$app->getFields()->saveField( $field );
Craft::$app->getFields()->saveField( $gallery );
谢谢你看这个,很抱歉是个疯子。