I'm trying to query webform submissions based on a field value. The field is called "ticket_for". Somethings I tried and do not work:

    ->condition('ticket_for',$adnummer)
    ->condition('webform_submission_data:ticket_for',$adnummer,'CONTAINS')
...

What kind of condition should I be using in a query like this?

$query = \Drupal::entityQuery('webform_submission')
->condition('webform_id', 'ticket')
->condition('entity_id', $eventID)
->condition('webform_submission_data:ticket_for',$adnummer,'CONTAINS');

Quoting @jrockowitz:

WebformSubmission data is not stored using Field API (see: #2792583).

You would have to query the webform_submission_data table and collect the submission ids (sid) or use the Webform Views module.

Source: Entity query webform submission data condition.

The table "webform_submission_data" uses the EAV model. To extract useful results you can use a static query.

Example SQL to the get sid meeting all the requirements:

SELECT DISTINCT sid
   FROM webform_submission_data wsd
     WHERE wsd.webform_id = 'ticket'
     AND sid IN (SELECT sid FROM webform_submission_data wsd2                   
                  WHERE wsd2.name = 'ticket_for'
                    AND wsd2.value CONTAINS '$adnummer')
     AND sid IN (SELECT sid FROM webform_submission_data wsd3
                  WHERE wsd3.name = 'entity_id'
                    AND wsd3.value = $eventID);

Example static query:

$query = $connection->query("SELECT DISTINCT sid
   FROM webform_submission_data wsd
     WHERE wsd.webform_id = 'ticket'
     AND sid IN (SELECT sid FROM webform_submission_data wsd2                   
                  WHERE wsd2.name = 'ticket_for'
                    AND wsd2.value CONTAINS :adnummer)
     AND sid IN (SELECT sid FROM webform_submission_data wsd3
                  WHERE wsd3.name = 'entity_id'
                    AND wsd3.value = :event_id)", [
  ':adnummer' => $adnummer,
  ':event_id' => $eventID
]);

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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