EntityMetadataWrapperException:设置文件字段时给出的数据值无效

时间:2014-08-22 15:41:23

标签: exception drupal-7 drupal-field-collection

无法在file_field中设置field_collection 节点$ order和field_collection是否为field_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()中(网站的第736行//所有/ modules / entity / includes / entity.wrapper.inc)。

我也尝试过:

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

1 个答案:

答案 0 :(得分: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;

以下是使用Entity metadata wrappers page中的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();
?>

当处理多值字段(基数&gt; 1)时,请确保将其包装到额外的数组中。