I am working on a project which involves passing data "profiles" via JSON to a web application. I am using Cakephp 3.0 and am very new to it. I store the profiles in a mysql database and can easily query for the data and put it into a basic JSON format with each row being a separate value in the JSON:
Controller.php:
....
public function getProfileData()
{
$uid = $this->Auth->user('id');
$this->loadComponent('RequestHandler');
$this->set('profile', $this->MapDisplay->find(
'all',
['conditions' =>
['MapDisplay.user_id =' => $uid]
]
)
);
$this->set('_serialize', ['profile']);
}
....
get_profile_data.ctp:
<?= json_encode($profile); ?>
Which returns something like this:
{
"profile": [
{
"alert_id": 1,
"alert_name": "Test",
"user_id": 85,
"initialized_time": "2017-03-24T00:00:00",
"forecasted_time": "2017-03-24T00:10:00",
"minimum_dbz_forecast": 0,
"maximum_dbz_forecast": 10,
"average_dbz_forecast": 5,
"confidence_in_forecast": 0.99,
"alert_lat": 44.3876,
"alert_lon": -68.2039
},
{
"alert_id": 1,
"alert_name": "Test",
"user_id": 85,
"initialized_time": "2017-03-24T00:00:00",
"forecasted_time": "2017-03-24T00:20:00",
"minimum_dbz_forecast": 5,
"maximum_dbz_forecast": 15,
"average_dbz_forecast": 10,
"confidence_in_forecast": 0.99,
"alert_lat": 44.3876,
"alert_lon": -68.2039
},
{
"alert_id": 2,
"alert_name": "Test2",
"user_id": 85,
"initialized_time": "2017-03-24T00:00:00",
"forecasted_time": "2017-03-24T00:10:00",
"minimum_dbz_forecast": 10,
"maximum_dbz_forecast": 20,
"average_dbz_forecast": 15,
"confidence_in_forecast": 0.99,
"alert_lat": 44.5876,
"alert_lon": -68.1039
},
{
"alert_id": 2,
"alert_name": "Test2",
"user_id": 85,
"initialized_time": "2017-03-24T00:00:00",
"forecasted_time": "2017-03-24T00:20:00",
"minimum_dbz_forecast": 15,
"maximum_dbz_forecast": 25,
"average_dbz_forecast": 35,
"confidence_in_forecast": 0.99,
"alert_lat": 44.5876,
"alert_lon": -68.1039
]
}
I am hoping to A) Easily call individual profiles instead of searching for unique profile ids and B) Only have to load one JSON file to get all profile contents. An output of something like this would be more ideal:
{
"profile": [
{
"alert_id": 1,
"alert_name": "Test",
"initialized_time":"2017-03-24T00:00:00",
"alert_lat": 44.3876,
"alert_lon": -68.2039,
"profile_data": [
{
"forecasted_time": "2017-03-24T00:10:00",
"minimum_dbz_forecast": 0,
"maximum_dbz_forecast": 10,
"average_dbz_forecast": 5,
"confidence_in_forecast": 0.99
},
{
"forecasted_time": "2017-03-24T00:20:00",
"minimum_dbz_forecast": 5,
"maximum_dbz_forecast": 15,
"average_dbz_forecast": 10,
"confidence_in_forecast": 0.99
}
]
},
{
"alert_id": 2,
"alert_name": "Test2",
"initialized_time": "2017-03-24T00:00:00",
"alert_lat": 44.5876,
"alert_lon": -68.1039,
"profile_data": [
{
"forecasted_time": "2017-03-24T00:10:00",
"minimum_dbz_forecast": 10,
"maximum_dbz_forecast": 20,
"average_dbz_forecast": 15,
"confidence_in_forecast": 0.99
},
{
"forecasted_time": "2017-03-24T00:20:00",
"minimum_dbz_forecast": 15,
"maximum_dbz_forecast": 25,
"average_dbz_forecast": 35,
"confidence_in_forecast": 0.99
}
]
}
]
}
How would I go about querying my database and populating this JSON structure? Are there any Cakephp tools that help do this? Does reframing the JSON into this structure seem to make sense?
Thanks in advance!