php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

One To Many Relation output an array

Open khkassem opened this issue 9 years ago • 4 comments

Hi

I have the folowing database : sections

and I am trying to get the sections with their corresponding department and directorate, the output is the following

{
  "sections": [
    {
      "ID": "1",
      "NAME": "section1",
      "DEPARTMENT_ID": "1",
      "departments": [
        {
          "ID": "1",
          "NAME": "depart 1",
          "DIRECTORATE_ID": "1",
          "directorates": [
            {
              "ID": "1",
              "NAME": "directorate1",
              "PHONE_NUMBER": "123",
              "ROOM_NUMBER": "123"
            }
          ],
          "EDL_ID": "65",
          "PHONE_NUMBER": "659",
          "ROOM_NUMBER": "987"
        }
      ],
      "PHONE_NUMBER": "654",
      "ROOM_NUMBER": "865"
    }
  ]
}

I think it will be better if the departments returned for the section is not an array, it can be directly the department object? a section has a department. and the same for the directorate in the department.

What do you think about this?

Thank you

test.txt

khkassem avatar Jul 13 '16 10:07 khkassem

Thank you for your feedback.

Note that the transformation is not intended to be executed on the server (&transform=1) as it defeats the purpose of a streaming API. It is intended that you run this client side (in JavaScript). You can find the file in "lib/php_crud_api_transform.js". You may make an alternative version that makes a difference between belongsTo and hasMany as you suggested.

Note that you may also want to take into account that there may be multiple references to department with different names and that you may want to use a different key to ensure it is at maximum one entry.

mevdschee avatar Jul 13 '16 12:07 mevdschee

Thank you I'll try to modify the crud_api_transform.js and let you know

khkassem avatar Jul 15 '16 07:07 khkassem

php api and crud_api_transform.js are really useful for me, thank you so much!!!!

todomato avatar Sep 21 '16 08:09 todomato

@todomato You are welcome, thank you for your feedback

mevdschee avatar Sep 21 '16 10:09 mevdschee