I am trying to achieve this code, which works correctly as a SQL consult on Phpmyadmin, in a PHP field of a View (Drupal 7).

FROM `field_data_field_qrespuesta_preg1` `r`
LEFT JOIN `field_data_field_qrespuesta_franquicia` `f` ON f.entity_id = r.entity_id
LEFT JOIN `field_data_field_qrespuesta_cuestionario` `c` ON c.entity_id = r.entity_id
WHERE `field_qrespuesta_cuestionario_nid` = 3134
AND `field_qrespuesta_franquicia_nid` = 3139
AND `field_qrespuesta_preg1_value` = 0

Now that's what I'm trying with no success:

$query = db_select('field_data_field_qrespuesta_preg1', 'r');
$query->leftjoin('field_data_field_qrespuesta_franquicia', 'f', 'r.entity_id = f.entity_id');
$query->leftjoin('field_data_field_qrespuesta_cuestionario', 'c', 'f.entity_id = c.entity_id');
$query->condition('c.field_qrespuesta_cuestionario_nid', $nid '=')
    ->condition('f.field_qrespuesta_franquicia_nid', $franquicia, '=')
    ->condition('r.field_qrespuesta_preg1_value', $respuesta, '=')

Any help will be really appreciated!

UPDATE: I did it with db_query, but I would still like to know how should it be done with db_select.

$query = db_query(
  'SELECT COUNT(*) FROM {field_data_field_qrespuesta_preg1} AS r ' .
  'LEFT JOIN {field_data_field_qrespuesta_franquicia} AS f ' .
  'ON r.entity_id = f.entity_id ' .
  'LEFT JOIN {field_data_field_qrespuesta_cuestionario} AS c ' .
  'ON f.entity_id = c.entity_id ' .
  'WHERE c.field_qrespuesta_cuestionario_nid = :nid ' .
  'AND f.field_qrespuesta_franquicia_nid = :franquicia ' .
  'AND r.field_qrespuesta_preg1_value = :respuesta',
    ':nid' => $nid,
    ':franquicia' => $franquicia,
    ':respuesta' => $respuesta,
  • 1
    "I'm trying with no success" - what you expected? What you got? Also, see drupal.stackexchange.com/q/128086/16495 – Mołot Dec 15 '14 at 12:17
  • I just got a blank page, instead of the view with the field displaying the resulting value from the query. I have never used db_select before so I assume I am making a syntactic mistake. Thanks for your comment! – unamaria Dec 15 '14 at 12:45
  • Blank page like WSOD? For a wsod, you should provide errors from server's error log and Drupal's watchdog. If there are not any, change logging settings to get them. See here: drupal.stackexchange.com/q/7560/16495 – Mołot Dec 15 '14 at 12:48
  • I guess leftJoin is to be written with a capital J. In lowercase it is not defined. – Neograph734 Dec 15 '14 at 14:20
  • @Neograph734 bad guess, I'm afraid. PHP was always case-insensitive on methods. Unless it was fixed and I missed that fact? I care about case anyway. – Mołot Dec 15 '14 at 14:55

Your Answer

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

Browse other questions tagged or ask your own question.