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 steps relations? #21

Closed
henninghall opened this issue Feb 8, 2016 · 19 comments
Closed

2 steps relations? #21

henninghall opened this issue Feb 8, 2016 · 19 comments
Assignees
Labels

Comments

@henninghall
Copy link

I am trying to attach relation data which works great except when it is "relations relation". In the example it should be possible to use mysql_crud_api_tranform() to achieve this.

I want to achieve this:

{ "posts": [ { "id": "1", "post_tags": [ { "id": "1", "post_id": "1", "tag_id": "1", "tags": [ { "id": "1", "name": "funny" } ] }, { "id": "2", "post_id": "1", "tag_id": "2", "tags": [ { "id": "2", "name": "important" } ] } ], "comments": [ { "id": "1", "post_id": "1", "message": "great" }, { "id": "2", "post_id": "1", "message": "fantastic" } ], "user_id": "1", "category_id": "1", "categories": [ { "id": "1", "name": "anouncement" } ], "content": "blog started" } ] }

But the result is :

{ "posts": [ { "id": "1", "post_tags": [ { "id": "1", "post_id": "1", "tag_id": "1", }, { "id": "2", "post_id": "1", "tag_id": "2", "tags": [ { "id": "2", "name": "important" } ] } ], "comments": [ { "id": "1", "post_id": "1", "message": "great" }, { "id": "2", "post_id": "1", "message": "fantastic" } ], "user_id": "1", "category_id": "1", "categories": [ { "id": "1", "name": "anouncement" } ], "content": "blog started" } ] "tags": [ { "id": "1", "name": "funny" } ] }

Do you know why or how i can fix this? If anyone is achieving the expected result, please provide an example of how you use mysql_crud_api_tranform(). Thanks for a great api!

@mevdschee
Copy link
Owner

Is that really the result? That is worrying as it is not even valid JSON. Can you confirm this?

How did you get this result? Please provide as much details as possible.

@mevdschee mevdschee added the bug label Feb 8, 2016
@mevdschee mevdschee self-assigned this Feb 8, 2016
@Disane87
Copy link

Disane87 commented Feb 9, 2016

Yep I have the same issue @mevdschee. I have a db as this model:
bildschirmfoto 2016-02-09 um 12 19 41

If I want to read one row of "RezeptMischungen", I have this structure by your api:
{ "RezepteMischungen": [ { "ID": "1", "Rezept_ID": "1", "Aroma_ID": "1" } ], "rezeptemischungen": [ { "ID": "1", "Rezept_ID": "1", "Rezepte": [ { "ID": "1", "Name": "Erdbeer", "Datum": "2016-02-09 12:00:42", "User_ID": "1", "Base_ID": "1", "Bewertung_ID": "2", "Reifezeit": "7" } ], "Aroma_ID": "1", "Aromen": [ { "ID": "1", "Name": "Erdbeer", "Datum": "2016-02-09 12:01:29", "Hersteller_ID": "1", "Bewertung_ID": "1" } ] } ], "Basen": [ { "ID": "1", "Name": "VPG", "Datum": "2016-02-09 11:43:19", "Hersteller_ID": "1", "Bewertung_ID": "2" }, { "ID": "2", "Name": "Traditional", "Datum": "2016-02-09 12:14:20", "Hersteller_ID": "1", "Bewertung_ID": "1" } ] }

Formated image of the return of your api:
bildschirmfoto 2016-02-09 um 12 24 31

My call of your api:
api.php/RezepteMischungen,Aromen,Rezepte,Basen?filter=rezept_id,eq1&callback=callback

Which is generally ok, but the table "Rezepte" has a relation to "Basen" which isn't applicated.
As you can see, there is a third level of a relation between RezepteMischung -> Rezepte -> Basen
I hope you know what I mean. If not, feel free to ask me anything.

Attached I send you my sql dump.
d020a380.txt

I expect a result like this:
bildschirmfoto 2016-02-09 um 12 38 35

Hope thats detailed enough inout for your debugging.
BTW: I'm using php_crud_api_transform.js

@mevdschee
Copy link
Owner

Thank you very much! I will look into this and report back after reproducing your findings.

mevdschee added a commit that referenced this issue Feb 9, 2016
Fix issue #21
@mevdschee
Copy link
Owner

Does this help?

{
    "RezepteMischungen": [
        {
            "ID": "1",
            "Rezept_ID": "1",
            "Rezepte": [
                {
                    "ID": "1",
                    "Name": "Erdbeer",
                    "Datum": "2016-02-09 12:00:42",
                    "User_ID": "1",
                    "Base_ID": "1",
                    "Basen": [
                        {
                            "ID": "1",
                            "Name": "VPG",
                            "Datum": "2016-02-09 11:43:19",
                            "Hersteller_ID": "1",
                            "Bewertung_ID": "2"
                        }
                    ],
                    "Bewertung_ID": "2",
                    "Reifezeit": "7"
                }
            ],
            "Aroma_ID": "1",
            "Aromen": [
                {
                    "ID": "1",
                    "Name": "Erdbeer",
                    "Datum": "2016-02-09 12:01:29",
                    "Hersteller_ID": "1",
                    "Bewertung_ID": "1"
                }
            ]
        }
    ]
}

And why do you get the table name both in mixed and in lower case? I can not reproduce that bug

@Disane87
Copy link

Disane87 commented Feb 9, 2016

I really don't know why. Could be a collation problem I guess. I will cross check that and your solution tomorrow. Thanks for you effort! :)

@mevdschee
Copy link
Owner

Okay, your contribution is much appreciated. You are probably running MySQL on Windows, right?

@Disane87
Copy link

Disane87 commented Feb 9, 2016

Oh no. Im running mysql within XAMPP under OSX El Capitan. I can check that with my external Apache with MySQL. But I created that DB with Navicat not PHPmyadmin.

@mevdschee
Copy link
Owner

@Disane87 Thank you for your cooperation on this second bug.

Please post the returned JSON on your machine and please also post the output of:

SELECT 1 as `N`,`TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'RezepteMischungen' UNION
SELECT 2,`TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'RezepteMischungen' UNION
SELECT 3,`TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` COLLATE 'utf8_bin' LIKE 'RezepteMischungen'  UNION
SELECT 4,`TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` LIKE 'rezeptemischungen';

on my machine it is:

N,TABLE_NAME
1,RezepteMischungen
2,RezepteMischungen
3,RezepteMischungen
4,RezepteMischungen

because:

Suppose that a query searches the SCHEMATA.SCHEMA_NAME column for the test database. On Linux, file systems are case sensitive, so comparisons of SCHEMATA.SCHEMA_NAME with 'test' match, but comparisons with 'TEST' do not:
...
On Windows or OS X where file systems are not case sensitive, comparisons match both 'test' and 'TEST':

see: https://dev.mysql.com/doc/refman/5.1/en/charset-collation-information-schema.html

Your help is very valuable!

@henninghall
Copy link
Author

Sorry my example wasn't the exact result. @Disane87 explains the same issue perfectly. I am using MySQL with XAMPP on Windows.

@mevdschee
Copy link
Owner

@henninghall You can update your "api.php" with the latest version as I applied a fix. Please let me know whether or not it works for you (it should).

@henninghall
Copy link
Author

@mevdschee It works brilliantly! Thanks a lot for keeping this api so great!

@mevdschee
Copy link
Owner

@Disane87 Can you still help me to identify the other bug (with the casing) by sending the returned JSON and executing the SQL queries on your machine? I have no OSX to test on here.

@Disane87
Copy link

@mevdschee
My result of your script:

N   TABLE_NAME
1   RezepteMischungen
2   rezeptemischungen
3   RezepteMischungen
4   RezepteMischungen

Returned JSON with your updated api.php:

{
    "RezepteMischungen": [
        {
            "ID": "1",
            "Rezept_ID": "1",
            "Rezepte": [
                {
                    "ID": "1",
                    "Name": "Erdbeer",
                    "Datum": "2016-02-09 12:00:42",
                    "User_ID": "1",
                    "Base_ID": "1",
                    "Bewertung_ID": "2",
                    "Reifezeit": "7"
                }
            ],
            "Aroma_ID": "1"
        }
    ],
    "rezepte": [
        {
            "ID": "1",
            "Name": "Erdbeer",
            "Datum": "2016-02-09 12:00:42",
            "User_ID": "1",
            "Base_ID": "1",
            "Basen": [
                {
                    "ID": "1",
                    "Name": "VPG",
                    "Datum": "2016-02-09 11:43:19",
                    "Hersteller_ID": "1",
                    "Bewertung_ID": "2"
                }
            ],
            "Bewertung_ID": "2",
            "Reifezeit": "7"
        }
    ],
    "rezeptemischungen": [
        {
            "ID": "1",
            "Rezept_ID": "1",
            "Aroma_ID": "1",
            "Aromen": [
                {
                    "ID": "1",
                    "Name": "Erdbeer",
                    "Datum": "2016-02-09 12:01:29",
                    "Hersteller_ID": "1",
                    "Bewertung_ID": "1"
                }
            ]
        }
    ]
} 

@mevdschee
Copy link
Owner

Ah.. thank you very much! Very unexpected to me!

SELECT `TABLE_NAME` FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` COLLATE 'utf8_bin' = 'RezepteMischungen';

Can you post the output of this SQL query as well?

@Disane87
Copy link

Sure :)

TABLE_NAME
RezepteMischungen

@mevdschee
Copy link
Owner

Thank you, I will prepare a new version tonight that should fix the bug.

@Disane87
Copy link

Thank you! :)
I will use your API for a bigger project, so I guess we'll stay in contact. But very great work! Really appreciate it!

@mevdschee
Copy link
Owner

The case sensitivity issue should also be fixed now. See: 7bd9919

@Disane87
Copy link

Thanks, works now!

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

No branches or pull requests

3 participants