zofe / rapyd-laravel

deprecated rewritten in rapyd-livewire
MIT License
866 stars 297 forks source link

DataFilter adding a closure to manipulate query #22

Closed zofe closed 10 years ago

zofe commented 10 years ago

DataFilter is good enough as is, but in some cases a closure on fields or main class can help to build better/complex filters. Another idea is to bind datafilter with eloquent query-scopes.

Some Ideas:

$filter->add('search','Find Something', 'text')->scope(function ($query, $value)  {
      return  $query->whereRaw("MATCH(`title`, `descr`, `tags`) AGAINST (?)", array($value));  
});

Or eloquent query scope binding:

$filter = DataFilter::source(new Article);
$filter->add('search','Find Something', 'text')->scope("search");

..
class Article extends Eloquent 
{
  ...
  public function scopeSearch($query, $value) 
  {        
          return $query->whereRaw("MATCH(`title`, `descr`, `tags`) AGAINST (?)", array($value));  
  }
..

Note that $value, it's not just Input::get('search'); It is $filter->field('search')->getValue(); So for complex or multiple fields (like autocomplete, date, tags..) it's a pre-processed value. For example for a Date field that use a custom format $value is instead a iso datestamp Y-m-d, so ready to be used in queries.

zofe commented 10 years ago

done.. (both)

larylinz commented 10 years ago

I use $filter->add('person.dob','Geburtsdatum', 'date')->format('d.m.Y', 'de'); But no mathes because the date ist not beeing convertet back to Y-m-d before qerying the database. I'm I doing something wrong?

zofe commented 10 years ago

this is not related with this issue, however accepted format for date field are related to the used datepicker
https://github.com/zofe/rapyd-laravel/blob/master/src/DataForm/Field/Date.php#L72 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format

and "dots" can cause problems in QS I think.. however can you try to find the problem?

farynato commented 9 years ago

I've also tried to add a date field in DataFilter and I've noticed that there is not an automatic conversion to Y-m-d to compare the value with DB, in contrast to daterange, where it exists. I think that date fields need a getValue function like datarange fields (https://github.com/zofe/rapyd-laravel/blob/master/src/DataForm/Field/Daterange.php#L15) to make this conversion. What do you think about this?

zofe commented 9 years ago

as said in thread to transform a date from "input" to the standard iso datestamp the Date field must know your input format. So you must use format() and be also sure that your format notation is supported by javascript datepicker: https://github.com/zofe/rapyd-laravel/blob/master/src/DataForm/Field/Date.php#L72 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format

and documented in the wiki, see date field: https://github.com/zofe/rapyd-laravel/wiki/Field-list

zofe commented 9 years ago

@farynato if you're sure that you're working right and DataFilter does not support Date field open a new issue and be propositive about how to solve the problem. (pull request, or suggestions are welcome)

farynato commented 9 years ago

Sorry for not specify, but I'm using: $filter->add('fecha_nacimiento','DOB','date')->format('d/m/Y', 'es')

I think that the format is supported, however, the query executed is: select * from ratones where fecha_nacimiento LIKE '%05/05/2015%'

If I change the field to datarange, it works, so it's for this for I was saying about a getValue function in Date.php, like in Datarange.php

zofe commented 9 years ago

so please open a new issue, I hope to fix asap

farynato commented 9 years ago

@zofe Ok, I will open a new issue I'll try to be propositive, but it's still a little difficult for me because I am a beginner in laravel. Thank you very much.