Drupal Views API: using $query->add_where_expression() in a loop

Status
Not open for further replies.

Lord Pachelbel

Ars Praefectus
5,499
Subscriptor++
In a custom module I'm using hook_views_query_alter() to add a number of complex WHERE expressions that Views UI can't handle.

One of them involves comparing Date fields related to temporary job openings. Originally the Date field in the job opening nodes allowed only one start/end date combination, but now the field needs to allow unlimited start/end date combinations. So I'm trying to alter the code to handle this situation.

Here is the original code:
Code:
// $job_opening_wrapper is an EntityMetadataWrapper
// $job_opening_wrapper->field_date is the Date field with only one start and end date.

$job_opening_date_start = $job_opening_wrapper->field_date->value()['value'];
$job_opening_date_end = $job_opening_wrapper->field_date->value()['value2'];

$query->add_where_expression(
    'date_search',
    "field_weekly_schedule_day = (DAYOFWEEK(:job_opening_date_start) - 1) OR
    DATE(field_one_off_availability_value) = DATE(:job_opening_date_start) OR
    DATE(field_one_off_availability_value2) = DATE(:job_opening_date_end)",
    array(
        ':job_opening_date_start' => $job_opening_date_start,
        ':job_opening_date_end' => $job_opening_date_end,
    )
);
Here's my new version:
Code:
$date_itr = 0;
foreach($job_opening_wrapper->field_date as $job_date) {

    $job_opening_date_start = $job_date->value()['value'];
    $job_opening_date_end = $job_date->value()['value2'];

    $query->add_where_expression(
        "date_search_{$date_itr}",
        "field_weekly_schedule_day = (DAYOFWEEK(:job_opening_date_start) - 1) OR
            DATE(field_one_off_availability_value) = DATE(:job_opening_date_start) OR
            DATE(field_one_off_availability_value2) = DATE(:job_opening_date_end)",
        array(
            ':job_opening_date_start' => $job_opening_date_start,
            ':job_opening_date_end' => $job_opening_date_end,
        )
    );
    $date_itr++;
}
My intention is for the final WHERE clause to look like this:
Code:
( (
field_weekly_schedule_day = (DAYOFWEEK('2017-02-21 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-21 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-21 23:00:00')

) )AND( (

field_weekly_schedule_day = (DAYOFWEEK('2017-02-22 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-22 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-22 23:00:00')

) )AND( (

field_weekly_schedule_day = (DAYOFWEEK('2017-02-23 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-23 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-23 23:00:00')

) )
The problem is even though each time through the loop the correct values are put into $job_opening_date_start and $job_opening_date_end, the final query simply uses the first date three times:
Code:
( (
field_weekly_schedule_day = (DAYOFWEEK('2017-02-21 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-21 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-21 23:00:00')

) )AND( (

field_weekly_schedule_day = (DAYOFWEEK('2017-02-21 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-21 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-21 23:00:00')

) )AND( (

field_weekly_schedule_day = (DAYOFWEEK('2017-02-21 14:00:00') - 1) OR
    DATE(field_one_off_availability_value) = DATE('2017-02-21 14:00:00') OR
    DATE(field_one_off_availability_value2) = DATE('2017-02-21 23:00:00')

) )
What am I doing wrong?
 
Status
Not open for further replies.