0

I want to sort view results basing on the selected start and end dates using exposed filters. So I want to AND the conditions in the WHERE clause. How can I do this?

I tried the following code, but it kills the query.

$param1 = \Drupal::request()->query->get('created_1');
$param2 = \Drupal::request()->query->get('created');
$param1_date = new DateTime($param1);
$param1_timestamp = $param1_date->getTimestamp();
$param2_date = new DateTime($param2);
$param2_timestamp = $param2_date->getTimestamp();
echo 'Start Date ' . $param1_timestamp . ' End Date ' . $param2_timestamp;
  
foreach ($query->where as &$condition_group) {  
  foreach ($condition_group['conditions'] as &$condition) {
    $query->setWhereGroup('AND');
    $query->addWhere('commerce_order.created', $param1_timestamp, '>=');
    $query->addWhere('commerce_order.created',$param2_timestamp, '<=');
  }
}

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.