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

Inheritance breaks filtering if relations are included #3610

Closed
g4b0 opened this issue Nov 7, 2014 · 7 comments
Closed

Inheritance breaks filtering if relations are included #3610

g4b0 opened this issue Nov 7, 2014 · 7 comments

Comments

@g4b0
Copy link
Contributor

g4b0 commented Nov 7, 2014

Here you are a test case:

class Veicle extends DataObject {

    private static $db = array(
            'Name' => 'Varchar(255)',
    );
    private static $has_one = array(
            'Brand' => 'Brand'
    );
    private static $summary_fields = array(
            'Name',
            'Brand.Name'
    );

}

class Brand extends DataObject {

    private static $db = array(
            'Name' => 'Varchar(255)'
    );

}

class Car extends Veicle {

    private static $db = array(
            'NumberOfDoors' => 'Int',
            'HasSunroof' => 'Boolean',
    );

}

class Truck extends Veicle {

    private static $db = array(
            'CargoCapacity' => 'Decimal'
    );

}

class DoAdmin extends ModelAdmin {

    private static $managed_models = array(
            'Brand',
            'Car',
            'Truck'
    ); // Can manage multiple models
    private static $url_segment = 'do-admin';
    private static $menu_title = 'DO Admin';

}

After a dev/build?flush=all you can access "DO Admin" -> "Car" section, and in the "Filter" form will appear "Brand Name" input box. The bug will be triggered when searching for some Brand.Name, and will output the following:

[User Error] Couldn't run query: SELECT DISTINCT count(DISTINCT "Veicle"."ID") AS "0" FROM "Veicle" LEFT JOIN "Car" ON "Car"."ID" = "Veicle"."ID" LEFT JOIN "Brand" ON "Brand"."ID" = "Car"."BrandID" WHERE ("Brand"."Name" LIKE '%ferrari%') AND ("Veicle"."ClassName" IN ('Car')) Unknown column 'Car.BrandID' in 'on clause'

Maybe this issue could be related to #2263

@sminnee
Copy link
Member

sminnee commented Nov 9, 2014

@tractorcow any thoughts? It would be great if we could squeeze these edge-case bugs out of our ORM. :-)

@g4b0
Copy link
Contributor Author

g4b0 commented Nov 10, 2014

I'm trying to fix this bug, but I can't find which part of code is responsable for adding the JOIN clause. I'm debugging into SearchFilter, but i got lost...

Any hint on where to search?

@dhensby
Copy link
Contributor

dhensby commented Nov 10, 2014

I think this is related to #3528 and #3556

You want to look at applyRelation

@g4b0
Copy link
Contributor Author

g4b0 commented Nov 10, 2014

I hope my fix will not have side effect somewhere into the code, after some first test it seems working.
Thanks a lot to @kinglozzer and @dhensby for the help

@tractorcow
Copy link
Contributor

I think @g4b0's fix is exactly the solution. It just needs to include the failing test cases and it's done.

@g4b0
Copy link
Contributor Author

g4b0 commented Nov 11, 2014

Really strange, I'm running the tests proposed in arambalakjian/silverstripe-framework@7f502ac , but I get this error:

There was 1 failure:

1) DataQueryTest::testApplyReplationDeepInheretence

Failed asserting that 

'SELECT DISTINCT "DataQueryTest_C"."ClassName", "DataQueryTest_C"."Created", "DataQueryTest_C"."LastEdited", "DataQueryTest_C"."Title", "DataQueryTest_C"."TestAID", "DataQueryTest_C"."TestBID", "DataQueryTest_E"."SortOrder", "DataQueryTest_C"."ID", CASE WHEN "DataQueryTest_C"."ClassName" IS NOT NULL THEN "DataQueryTest_C"."ClassName" ELSE 'DataQueryTest_C' END AS "RecordClassName" FROM "DataQueryTest_C" LEFT JOIN "DataQueryTest_E" ON "DataQueryTest_E"."ID" = "DataQueryTest_C"."ID" LEFT JOIN "DataQueryTest_A" ON "DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID" WHERE ("DataQueryTest_C"."ClassName" IN ('DataQueryTest_E')) ORDER BY "DataQueryTest_E"."SortOrder" ASC' 

contains 

""DataQueryTest_A"."ID" = DataQueryTest_C"."TestAID""

@g4b0
Copy link
Contributor Author

g4b0 commented Nov 11, 2014

Ok, I found the issue: there was a missing " in the assertContains needle. Now tests are OK

g4b0 added a commit to g4b0/silverstripe-framework that referenced this issue Nov 11, 2014
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

No branches or pull requests

4 participants