l5-repository icon indicating copy to clipboard operation
l5-repository copied to clipboard

parserFieldsSearch method has a bug, please fixed it.

Open couth opened this issue 5 years ago • 2 comments

  1. These are some codes.

`

class ConfigRepositoryEloquent extends BaseRepository implements ConfigRepository
{
    protected $fieldSearchable = [
        'name',
    ];

  //…

` 2. I use these query params

`

?search=hello&searchFields=namee:like

`

  1. The flow errors occurs.

`

{ "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'configs.namee' in 'where clause' (SQL: select * from `configs` where (`configs`.`namee` like %hello%)…

`

  1. So, I read the code about parserFieldsSearch method in Prettus\Repository\Criteria\RequestCriteria. I think it has a bug, and I use the follow code to fixed it. I hope you could fixed the bug soon.

`

protected function parserFieldsSearch(array $fields = [], array $searchFields = null)
{
    if (!is_null($searchFields) && count($searchFields)) {
        $acceptedConditions = config('repository.criteria.acceptedConditions', [
            '=',
            'like'
        ]);
        $originalFields = $fields;
        $fields = [];
        
        //these are my codes begin
        foreach ($searchFields as $field) {
            $field_parts = explode(':', $field);
            if(in_array($field_parts[0], $originalFields)) {
                if(count($field_parts) == 2 && in_array($field_parts[1], $acceptedConditions)) {
                    $fields[$field_parts[0]] = $field_parts[1];
                } else {
                    $fields[$field_parts[0]] = '=';
                }
            }
        }
        // these are my codes end

        if (count($fields) == 0) {
            throw new \Exception(trans('repository::criteria.fields_not_accepted', ['field' => implode(',', $searchFields)]));
        }

    }

    return $fields;
}

`

  1. I thik the follow message is what we expect.

`

{ "message": "repository::criteria.fields_not_accepted", "exception": "Exception", "file": "D:\\www\\hero-api\\app\\Criteria\\BaseRequestCriteria.php", 

`

couth avatar Jun 29 '19 05:06 couth

In the $fieldsSearchable you declare "name" while in the search you search for "namee".

pvmerlo avatar Jul 10 '19 21:07 pvmerlo

i have same issue, but in filter that not has a valid column

lloricode avatar Aug 07 '19 01:08 lloricode