I'm confused by the field_collection API. I created a Field Collection and attached it to a node (in the GUI) but I can't figure out how to code an update/addition to the field collection. This is my scenario:
I have a node (request) that has product information and quantities. When users go to a product page they see a link to "Add to Request". This link should add the product (nid) to the node request. The difficulty lies in the fact that I need quantity information as well, hence the Field Collection. I added a quantities field and grouped it with the product field.
Node Type: request
Request ID: $requestid
Product ID: $productid
Field Collection = field_request_collection (Unlimited values)
Field Collection Field = field_products (Entity Reference, Max 1)
Field Collection Field = field_product_quantities (Integer, Max 1, Default 1)
My function looks like this (the values for $requestid and $productid are passed in the URL of the link: /my_request/5/12):
function _my_module_request_update($requestid,$productid) {
$nload = node_load($requestid);
$field_collection_item_value = $nload->field_request_collection[LANGUAGE_NONE][0]['value'];
$field_collection_item = entity_load('field_collection_item', array($field_collection_item_value));
$field_collection_item->field_products[LANGUAGE_NONE][0]['value'] = intval($productid);
$field_collection_item->save();
}
The error I'm getting with the above code is:
PHP Fatal error: Call to a member function
save()
on a non-object
Keep in mind that I'm attempting to ADD Field Collection values NOT replace them. Please help, thanks!
entity_load()
is failing. What do you get for the$field_collection_item
object when you inspect it?