I'm creating a field collection item and saving it via entity_create, and it all works great as long as I don't have an image tied to it. As soon as I do, I get an error, and it looks like this in the watchdog table:

    a:6:{s:5:"%type";s:12:"PDOException";s:8:"!message";s:516:"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fid' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array
    [:db_insert_placeholder_0] => 
    [:db_insert_placeholder_1] => file
    [:db_insert_placeholder_2] => field_collection_item
    [:db_insert_placeholder_3] => 50
    [:db_insert_placeholder_4] => 1

My code looks like this:

$values = array(
    'field_name' => 'field_bp_images',
    'field_bp_image_title' => array( LANGUAGE_NONE => array(array('value' => $title)), ),
    'field_bp_image_file' => array( LANGUAGE_NONE => array(array('value' => $fid)), ), 
    'field_bp_image_photographer_name' => array( LANGUAGE_NONE => array(array('value' => $photographer)), ),
    'field_bp_image_photo_flag' => array( LANGUAGE_NONE => array(array('value' => $is_photo)), ), 

$field_collection_item  = entity_create('field_collection_item', $values);
#Attach to the node
$field_collection_item->setHostEntity('node', $node); 

// # Save field-collection item.

My $fid is definitely populated, but I am still getting that error telling me my fid cannot be null. I'm guessing I need to provide more elements to the array, but not sure what keys to give, or how many. Any thoughts?


The column name for an image field is fid (not value), so you just need to change this bit of your $values array:

'field_bp_image_file' => array( LANGUAGE_NONE => array(array('fid' => $fid)), ),
  • Clive, Thanks once again, that seems to have helped, once. I was updating only one record. Subsequent tries with several records produced errors such as these: "Warning: Illegal string offset 'field' in DatabaseCondition->__clone() (line 1895 of C:\xampp\htdocs\LLIS-NPAD\includes\database\query.inc)." and "Recoverable fatal error: Object of class stdClass could not be converted to string". I'm unsure how to troubleshoot this, so I appreciate additional thoughts.
    – Jav
    May 29 '13 at 19:21
  • Clive, I'm still struggling with this. Any chance you can advice on how to find out what an image field expects? Just passing it the fid doesn't seem to be enough
    – Jav
    May 31 '13 at 2:43
  • Looks like I just needed to set the image up like this: 'field_bp_image_file' => array( LANGUAGE_NONE => array(array('fid' => $fid,'display'=>1,'description'=>'' )), ). Now to figure out which properties are required by word and other files, I can get this thing dones!
    – Jav
    Jun 4 '13 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.