0

I am trying to execute this query:

'SELECT sd.entity_id AS entity_id, WEEK(node__field_start_date.field_start_date_value) AS week, YEAR(node__field_start_date.field_start_date_value) AS year
FROM
{node__field_start_date} sd
INNER JOIN {node__field_plane_reservation} pr ON sd.entity_id = pr.entity_id
WHERE (sd.bundle = :db_condition_placeholder_0) AND (pr.field_plane_reservation_target_id = :db_condition_placeholder_1) AND (week = :db_condition_placeholder_2) AND (year = :db_condition_placeholder_3)'

And I am using this piece of code:

    $database = \Drupal::database();
    $query = $database->select('node__field_start_date', 'sd')
  ->fields('sd', ['entity_id']);

    // Expressions
    $query->addExpression('WEEK(sd.field_start_date_value)', 'week');
    $query->addExpression('YEAR(sd.field_start_date_value)', 'year');

    // Conditions
    $query
  ->condition('sd.bundle', 'reservation', '=')
  ->condition('pr.field_plane_reservation_target_id', $refPlane, '=')
  ->condition('week', $weekNo, '=')
  ->condition('year', $weekYr, '=');

    // Join Tables
    $query->join('node__field_plane_reservation', 'pr', 'sd.entity_id = pr.entity_id');

And whenever I execute it, it give me the error: Unknown Column 'week' in query or Unknown column 'year'. How do I use a condition with a pseudo column created with an expression?

1 Answer 1

1
 // Where Expressions
    $query->where('WEEK(sd.field_start_date_value) = :weekNo', array(':weekNo' => $weekNo));
    $query->where('WEEK(sd.field_start_date_value) = :weekYr', array(':weekYr' => $weekYr));

Apparently this can be achieved with $query->where();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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