Giter VIP home page Giter VIP logo

Comments (6)

baumelt avatar baumelt commented on September 27, 2024 1

Just testet everything with PHP v5.6, but the errors remained the same; the resulting sql query always treats the params as attributes.

I then tried to debug the APIHandler and was able to isolate the issue to the Parser->getFilterParams() Function.
At least for me the array_diff_ukey in Line 277 doesn't return a correct array of filter params for some reason i can't get my head around yet.

Parser.php - Line 272-286:

    protected function getFilterParams()
    {
        $reserved = array_fill_keys($this->functions, true);
        $prefix = $this->prefix;
        $filterParams = array_diff_ukey($this->params, $reserved, function ($a, $b) use ($prefix) {
            return $a != $prefix . $b;
        });
        if (count($filterParams) > 0) {
            return $filterParams;
        }
        return false;
    }

For Example when provided with _limitthe key will be removed from the filter params but the _sort key won't. It seems the function is not comparing all pairs of keys provided correctly.

To check this i added a simple echo output to the compare callback function as below:

        $filterParams = array_diff_ukey($this->params, $reserved, function ($a, $b) use ($prefix) {
            echo("\$a:" . $a . " != \$b:" . $prefix . $b . " | " . (($a != $prefix . $b) ? 'true' : 'FALSE') . "<br/>");
            return $a != $prefix . $b;
        });

Requesting: /api/users?_limit=1&_with=roles&_sort=name&email-lk=*com
the output then shows:

$a:offset != $b:_sort | true
$a:offset != $b:_limit | true
$a:offset != $b:_fields | true
$a:offset != $b:_config | true
$a:offset != $b:_with | true
$a:q != $b:_offset | true
$a:limit != $b:_sort | true
$a:limit != $b:_with | true
$a:limit != $b:_fields | true
$a:config != $b:_limit | true
$a:sort != $b:_fields | true
$a:with != $b:_sort | true
$a:_limit != $b:_limit | FALSE
$a:_with != $b:_limit | true
$a:_with != $b:_offset | true
$a:_with != $b:_q | true
$a:_sort != $b:_limit | true
$a:_sort != $b:_offset | true
$a:_sort != $b:_q | true
$a:email-lk != $b:_limit | true
$a:email-lk != $b:_offset | true
$a:email-lk != $b:_q | true

As you can see the _limit will successfully be removed, while the rest aren't compared to all predefined params and therefore will be passed on as filters resulting in my previous sql errors.


So, to make this work for now i tried the array_diff_key function to replace the ukey like so:

    protected function getFilterParams() {

        $_functions = ['_fields', '_sort', '_limit', '_offset', '_config', '_with', '_q'];
        $reserved = array_fill_keys($_functions, true);

        $prefix = $this->prefix;

        $filterParams = array_diff_key($this->params, $reserved);

        if (count($filterParams) > 0) {
            return $filterParams;
        }

        return false;
    }

.. and then _with, _sortand _fields instantly work as intended!

However, i can't believe or explain how i'm the only one having this issue and i've no idea how so :(
Perhaps you might be better suited to deduce how this might occur? I would really like to be able to use your project as vendor dependency instead of this hack, but it's at least some progress, right? :D'

I'm kind of confused what might be wrong with my php environment, since i tried this with clean laravel/homestead vagrant boxes, version 0.3.3 (PHP5.6.99) and 0.4.4. (PHP7.0.5)

Maybe this will at least be of some future help for someone having similar problems.

Edit: changed some spelling and few mistakes in the explanation

from laravel-api-handler.

marcelgwerder avatar marcelgwerder commented on September 27, 2024 1

I'll check this out and will try to reproduce it as soon as I can, thanks for investigating.

from laravel-api-handler.

marcelgwerder avatar marcelgwerder commented on September 27, 2024

There is no need to do anything more. Looks like somehow _sort and _with are not detected as predefined parameters. I used this code with many Laravel versions including 5.1 and never had such issues.

Can you post the code where you call the handler and the PHP version you're using?

from laravel-api-handler.

baumelt avatar baumelt commented on September 27, 2024

Hey Marcel,

thank you for the quick reply. Sadly i still haven't figured out what is causing my failure.

The Php Version that came with my vagrant seems to be PHP 7.0.5. Maybe that causes the issue?

Here are the relevant code snippets:

Post Controller:

public function index(Request $request) {

        // find all posts with selected nested relations
        $posts = Post::with('address');

        // remove unwanted params from request
        $searchParams = $request->except(['token']);

        $searchable = ['id', 'name', 'latitude', 'longitude'];

        try {

            // apply optional search query parameters as filter and return result
            return \ApiHandler::parseMultiple($posts, $searchable, $searchParams)->getResponse();

        } catch (Exception $e) {/*...*/}
    }

Post Model:

    /**
     * @Relation
     */
    public function address() {
        return $this->belongsTo(Address::class);
    }

    /**
     * @Relation
     */
    public function user() {
        return $this->belongsTo(User::class);
    }

When calling /api/posts?_with=user:

SQLSTATE[42S22]: Column not found: Unknown column '_with' in 'where clause' (SQL: select * from `posts` where `posts`.`deleted_at` is null and `_with` = user)

Im still kind of confused how most of the other _parameter work nicely. But maybe you get an idea with these snippets?

from laravel-api-handler.

marcelgwerder avatar marcelgwerder commented on September 27, 2024

Hmm I can't see an issue on the first glimpse. But I didn't really test the library against PHP 7 yet so It is possible that there is some different behaviour. Is it possible for you to test your code on PHP 5.6 without too much hassle?

from laravel-api-handler.

marcelgwerder avatar marcelgwerder commented on September 27, 2024

Using the handler with many PHP 7+ versions, I cannot reproduce. If there is still a problem, the next major version will probably make it go away anyway.

from laravel-api-handler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.