【发布时间】:2015-01-08 18:35:30
【问题描述】:
我需要以编程方式创建一个节点。在我的内容类型中有一个字段集合,其中还有另一个字段集合。现在如何在内部字段集合中保存字段的值因此,由于这种嵌套的字段集合结构,我无法在此节点中保存字段集合项。
下面是我的代码:
global $user;
$node = entity_create('node', array('type' => 'key_sentence')); // replace entity_type and entity_bundle with your info
// Specify the author
$node->uid = $user->uid;
// Create a Entity Wrapper of that new Entity
$entity = entity_metadata_wrapper('node', $node);
// Specify the title
$entity->title = 'Test node';
// Add field data... SO MUCH BETTER!
$entity->field_key_sentence_text = 'Field value text';
$entity->save(); // necessary in order to attach field collections
//Outer field collection
$field_collection_1 = entity_create('field_collection_item', array('field_name' => 'field_ks_sent_to'));
// Set default values as necessary
$field_collection_1->setHostEntity('field_collection_item', $entity); // again, replace entity_type as appropriate
$field_collection_1->save(TRUE);
//Inner field collection
$field_collection_2 = entity_create('field_collection_item', array('field_name' => 'field_ks_in_voice'));
// Set default values as necessary
$field_collection_2->setHostEntity('field_collection_item', $field_collection_1);
//Field in inner field collection
$field_collection_2->field_message_url[LANGUAGE_NONE][0]['value'] = 'http://www.google.com';
$field_collection_2->save(TRUE);
$entity->save();
但我收到以下错误:
Notice: Undefined index: value in field_collection_field_update() (line 933 of /home/postcinn/public_html/dev/sites/all/modules/field_collection/field_collection.module).
EntityMalformedException: Missing bundle property on entity of type field_collection_item. in entity_extract_ids() (line 7766 of /home/postcinn/public_html/dev/includes/common.inc).
【问题讨论】:
标签: drupal-7