Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Update check for filtering column from include #70

Closed
wants to merge 6 commits into from

Conversation

Angelinsky7
Copy link
Contributor

if we try to use the filter method on a column based on an included properties like this :

{base_url}/users?filter[author.name]=Gentrit

the current implementation will crash because author.name won't be in the $whiteListFilter.
if we use this modification like this we can find a way to reuse properties :

class User extends Model
{
  use HasFactory;

  public function author()
  {
      return $this->belongsTo(Author::class);
  }

  public static $whiteListFilter = ['id', 'author'];
  public static $whiteListFilteType = [null, Author::class];
}
class Author extends Model
{
  use HasFactory;

  public static $whiteListFilter = ['id', 'name'];
}

The checkFilterColumn will check the first part of the $colum variables (author.name => author) and then check the next part (name => name) against the Author::class

Like this we can filter all properties and sub-properties.

@gentritabazi
Copy link
Collaborator

If we set: public static $whiteListFilter = ['author.name']; there is no crash/problem here ?

@Angelinsky7
Copy link
Contributor Author

Angelinsky7 commented May 7, 2021

yes but imagine that we have hundred of field in the sub-property and that this author object is used in hundred of other classes this becomes a nightmare to maintain.
with this solution we can define each properties only once

@gentritabazi
Copy link
Collaborator

Thank you very much for the contribution but I think this can be made more easier without whiteListFilteType.

@Angelinsky7
Copy link
Contributor Author

of course, but i didn't know how to make the checkFilterColumn find the correct class.

@gentritabazi
Copy link
Collaborator

Hmm...

author is relationship we can grab class from relationship 🚀

@Angelinsky7
Copy link
Contributor Author

Angelinsky7 commented May 7, 2021

oh ! i didn't know that (i'm pretty new to laravel) if you give me an hint to the documentation i'ld be happy to make the correct modification. (and now that i think about it, we could maybe handle both of the syntax, the author.name in the whitelist and the author)

@gentritabazi
Copy link
Collaborator

I think this is can be more easy for example if we want to filter all columns of author we can do something like:

$whiteListFilter = ['author.*'].

@Angelinsky7
Copy link
Contributor Author

i also like your idea and i could also add it.
but remains my issue : i would like to use the filters set in the author model without needing to care about anything else.
For example, each time i use the author model in a relationship i would like to be able to disable the possibility to filter by created_at or updated_at fields (it's an example)
My proposal is (i hope) resolving this.

@gentritabazi gentritabazi changed the base branch from master to 2.x May 8, 2021 20:27
@gentritabazi gentritabazi changed the title Update check for filtering column from include [2.x] Update check for filtering column from include May 9, 2021
@gentritabazi gentritabazi self-requested a review May 9, 2021 19:28
@gentritabazi
Copy link
Collaborator

Also ['*'] is perfect 🤩

@Angelinsky7
Copy link
Contributor Author

Angelinsky7 commented May 10, 2021

So the list of possible values could be

  • a field in the model
  • a star for all fields and relationships (and sub-fields) in the model
  • a relationship in the model (using sub-field of this relationship)
  • a relationship with a star (to use all field in the relationship)

@gentritabazi
Copy link
Collaborator

When you set ['*'] this is mean you can filter anything including relations.

@gentritabazi gentritabazi marked this pull request as draft May 14, 2021 20:20
@Angelinsky7 Angelinsky7 marked this pull request as ready for review May 26, 2021 17:46
@Angelinsky7
Copy link
Contributor Author

Angelinsky7 commented May 26, 2021

@gentritabazi01 normally everything is there now. i hope that everything will work correctly.

@gentritabazi
Copy link
Collaborator

Did you make tests locally for all cases ?

@Angelinsky7
Copy link
Contributor Author

Yes of course, but because we don't have unit tests i would gladly prefere that you also test it 😅

$nextColums = join('.', array_slice($parts, 1));
$baseClass = new $baseClassName();
$nextClass = method_exists($baseClass, $firstPart) ? get_class($baseClass->$firstPart()->getRelated()) : '';
// If the whiteListFilter contains a column with a star we want to bypass the check for the next part
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put extra line here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before line 320.

Comment on lines +7 to +36
```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
use HasFactory;

public function author()
{
return $this->belongsTo(Author::class);
}

// List of all valid syntax for $whiteListFilter
//public static $whiteListFilter = ['*'];
//public static $whiteListFilter = ['id', 'title', 'author'];
//public static $whiteListFilter = ['id', 'title', 'author.*'];

}
```

If the filter is `['*']` then all properties and sub-properties can be used for filtering.
If the filter is `a list of model properties` then only the selected properties can be filtered.
If some of the filter are a relationship then only the `$whiteListFilter` properties of the sub-property's model can be filtered.
If some of the filter contains a `.*` the all sub-properties of the relationship model can be filtered (the `$whiteListFilter` is not used).

Copy link
Collaborator

@gentritabazi gentritabazi Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To much code here,

Can you update like this:


List of all valid syntax for $whiteListFilter:

public static $whiteListFilter = ['*'];
public static $whiteListFilter = ['id', 'title', 'author'];
public static $whiteListFilter = ['id', 'title', 'author.*'];  

If the filter is ['*'] then all properties and sub-properties can be used for filtering.
If the filter is a list of model properties then only the selected properties can be filtered.
If some of the filter are a relationship then only the $whiteListFilter properties of the sub-property's model can be filtered.
If some of the filter contains a .* the all sub-properties of the relationship model can be filtered.

@gentritabazi gentritabazi marked this pull request as draft June 15, 2021 19:51
@gentritabazi
Copy link
Collaborator

This pull request has been close since there has not been any recent activity.

Please open a new pull request for related feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants