tombenner / wp-mvc

An MVC framework for WordPress
http://wpmvc.org
MIT License
624 stars 172 forks source link

Bug with nested conditions in model->find() #237

Closed amaneshi closed 5 years ago

amaneshi commented 5 years ago

In project I have such condition block in find options:

 'conditions' => [
                'OR' => [
                    [
                        'Game.team1_id' => 1,
                        'Game.team2_id' => 10,
                    ],
                    [
                        'Game.team1_id' => 10,
                        'Game.team2_id' => 1,
                    ]
                ]
            ]

According to logic I want to get:

((Game.team1_id = "1" AND Game.team2_id = "10") OR (Game.team1_id = "10" AND Game.team2_id = "1"))

but got (in first where clause I got OR in place of AND):

((Game.team1_id = "1" OR Game.team2_id = "10") OR (Game.team1_id = "10" AND Game.team2_id = "1"))

After investigating plugin code I found a bug. In MvcDatabaseAdapter class on line 115 we have:

    $logical_operator = ($key == 'OR') ? ' OR ' : ' AND ';

As in my case first sub-clause of OR clause have KEY = 0 PHP evaluates $key == 'OR' to 'true' but should be 'false'. To fix this bug we should use triple equality sign === to overcome this issue. Please fix this bug!

cyberscribe commented 5 years ago

Thanks for this. A pull request that has been carefully tested to ensure no side-effects would be most welcome.

amaneshi commented 5 years ago

OK, I'll create pull. About testing - I tested it on my case and theoretically it shouldn't broke anything. If you have another cases to test - point me.