Whats the best way to lookup data in Field Collections with EFQ?

I have a Field Collection with a couple of fields set to a host entity that contains a table of pricing information. When new prices are received from a third party service, I would like to query against the incoming option, locate it, and update its price.

Heres what I have been trying ($this->entity_id is a host entity ID, the other two $this values are assigned when looping the data):

$query = new EntityFieldQuery;
		$query->entityCondition('entity_type', 'field_collection_item')
			->entityCondition('bundle', 'field_trinkets')
			->entityCondition('entity_id', $this->entity_id);
		if (isset($this->trinket_option)) {
			$query->fieldCondition('field_trinket_option', 'value', $this->trinket_option, '=');
		if (isset($this->trinket_price)) {
			$query->fieldCondition('field_trinket_price', 'value', $this->trinket_price, '=');

                $trinket_record = $query->execute();

		if ($trinket_record) {
			$trinket_record = end(entity_load('field_collection_item', array_keys($trinket_record['field_collection_item'])));
			return $trinket_record;
		} else {
			return FALSE;

Either way, no records are ever found when there should be matches most of the time. What am I missing?


bengt’s picture

Any suggestions?

bengt’s picture

The answer (for me, at least) is found here: http://drupal.org/node/1353926 (especially the comments).

jilladams’s picture

Title: Locating data within Field Collections with EntityFieldQuery » Locating data within Field Collections with EntityFieldQuery (or EFQ Views)
Version: 7.x-1.0-beta3 » 7.x-1.0-beta4
Category: support » feature
Priority: Normal » Major

Bumping this, but in relation to EFQ Views. The Field Collection is only available as an entire entity, and the fields contained within can't be hooked/manipulated by the view.

jmuzz’s picture

Priority: Major » Normal
Issue summary: View changes
Alan D.’s picture

Version: 7.x-1.0-beta4 » 7.x-1.0-beta8
Status: Active » Closed (works as designed)

Um, no issues when I just had a play, I Googled first to save some typing / investigation & hit this and other blank responses...

Tested on beta8

    $efq = new EntityFieldQuery();
    $efq->entityCondition('entity_type', 'field_collection_item');

    // No bundle value, use field name
    $efq->propertyCondition('field_name', 'field_appointment_document');

    // I'm restricting to search items attached to something, so filtering by the FC I want to check.
    $efq->propertyCondition('item_id', $doc_fc_ids);

    // We don't want revisions!
    $efq->propertyCondition('archived', 0);

    // An example to filter by a field attached to the FC.
    $efq->fieldCondition('field_document_type', 'tid', array(123,432));

    // No date information or useful other property info, but I want the newest one only.
    // So highest ID is the newest, and only return one result.
    $efq->propertyOrderBy('item_id', 'DESC');
    $efq->range(0, 1);