From be03fafd0af5b1730a6fcc4afa19fc1bead6b212 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 23 Sep 2017 11:19:44 -0400 Subject: [PATCH] push residents onto location --- database/sql/locations/find.sql | 12 +++++++++++- models/location.php | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/database/sql/locations/find.sql b/database/sql/locations/find.sql index bdba063..38d1c9a 100644 --- a/database/sql/locations/find.sql +++ b/database/sql/locations/find.sql @@ -1 +1,11 @@ -SELECT * FROM locations +SELECT + locations.id AS location_id, + street, + city, + state, + people.id AS person_id, + name, + age +FROM locations +LEFT JOIN people ON locations.id = people.location_id +ORDER BY locations.id; diff --git a/models/location.php b/models/location.php index 52be2b8..03fbd74 100644 --- a/models/location.php +++ b/models/location.php @@ -1,5 +1,6 @@ id), $data->street, $data->city, $data->state); + if($current_location === null || $current_location->id !== intval($data->location_id)){ + $current_location = new Location(intval($data->location_id), $data->street, $data->city, $data->state); + $current_location->residents = []; + $locations[] = $current_location; + } + if($data->person_id){ + $new_person = new Person(intval($data->person_id), $data->name, intval($data->age)); + $current_location->residents[] = $new_person; + } + } return $locations;