ClanCats / Hydrahon

🐉 Fast & standalone PHP MySQL Query Builder library.
https://clancats.io/hydrahon/master/
MIT License
278 stars 58 forks source link

Metodos MONTH como usar ? #57

Closed vsalgueiro7 closed 1 year ago

vsalgueiro7 commented 3 years ago

como eu usaria essa query

SELECT * FROM despesas WHERE MONTH(des_vencimento) = MONTH(CURRENT_DATE()) AND YEAR(des_vencimento) = YEAR(CURRENT_DATE())

vsalgueiro7 commented 3 years ago

encontrei .. srs ..

use ClanCats\Hydrahon\Query\Expression as Ex;

hugo-leij commented 1 year ago

Hi @vsalgueiro7

How do you use it?

I got:

      $traffic = $h->table('duration_in_traffic_data');
      $result = $traffic
        ->select()
        ->where(function($q) {
          $q->where('datetime', '>=', $_GET['date_from']);
          $q->where('datetime', '<=', $_GET['date_to']);
        })
        ->get();

This gives a error:

      $traffic = $h->table('duration_in_traffic_data');
      $result = $traffic
        ->select()
        ->where(function($q) {
          $q->where('DATE(datetime)', '>=', $_GET['date_from']);
          $q->where('DATE(datetime)', '<=', $_GET['date_to']);
        })
        ->get();

How can u make DATE(datetime) ?

mario-deluna commented 1 year ago

@hugo-leij @vsalgueiro7 You need to mark expressions as expressions otherwise Hydrahon does not know when to not escape your fields:

use ClanCats\Hydrahon\Query\Expression as Ex;

$traffic = $h->table('duration_in_traffic_data');
$result = $traffic
  ->select()
  ->where(function($q) {
    $q->where(new Ex('DATE(datetime)'), '>=', $_GET['date_from']);
    $q->where(new Ex('DATE(datetime)'), '<=', $_GET['date_to']);
  })
  ->get();