1

Has ノード $order および field_collectionfile_fieldで設定できません:field_collectionfield_blueprints

<?php
$entity_type = "field_collection_item";
$blueprint_obj = entity_create($entity_type, array('field_name' => "field_blueprints") );
$blueprint_obj->setHostEntity('node', $order);
$blueprint_entity = entity_metadata_wrapper($entity_type, $blueprint_obj);
date_default_timezone_set("UTC");
$blueprint_entity->field_blueprint_file->file->set((array)$file);
$blueprint_entity->field_blueprint_comment = (string) $file->filename;
$blueprint_obj->save();
node_save($order);

そして、このコードはエラーをスローします:

EntityMetadataWrapperException: 無効なデータ値が指定されました。必要なデータ型と形式に一致していることを確認してください。( EntityDrupalWrapper->set()sites//all/modules/entity/includes/entity.wrapper.inc の 736 行目)。

私も試しました:

$blueprint_entity->field_blueprint_file->set((array)$file)
$blueprint_entity->field_blueprint_file->set(array('fid'=>$file->fid))
4

1 に答える 1

0

機能させるには、ファイル オブジェクトまたはfidキーを含む配列を渡す必要があります。

したがって、次のいずれかです。

// Single value field
$blueprint_entity->field_blueprint_file = array('fid' => $file->fid);
// Multi-value field
$blueprint_entity->field_blueprint_file[] = array('fid' => $file->fid);

また:

// Single value field
$blueprint_entity->field_blueprint_file = $file;
// Multi-value field
$blueprint_entity->field_blueprint_file[] = $file;

を使用した完全な例とvalue()エンティティ メタデータ ラッパー ページからの例を次に示します。set()save()

<?php
  $containing_node = node_load($nid);
  $w_containing_node = entity_metadata_wrapper('node', $containing_node);

  // Load the file object in any way
  $file_obj = file_load($fid);
  $w_containing_node->field_attachment_content->file->set( $file_obj );
  // ..or pass an array with the fid
  $w_containing_node->field_attachment_content->set( array('fid' => $fid) );
  $w_containing_node->save();
?>

また、複数の値を持つフィールド (カーディナリティ > 1) を扱う場合は、必ず追加の配列にラップしてください。

于 2016-03-15T00:04:45.990 に答える