php-crud-api
php-crud-api copied to clipboard
One To Many Relation output an array
Hi
I have the folowing database :

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
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.
Thank you I'll try to modify the crud_api_transform.js and let you know
php api and crud_api_transform.js are really useful for me, thank you so much!!!!
@todomato You are welcome, thank you for your feedback