push residents onto location

master
Matt Huntington 8 years ago
parent 87e70da120
commit be03fafd0a

@ -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;

@ -1,5 +1,6 @@
<? <?
include_once __DIR__ . '/../database/db.php'; include_once __DIR__ . '/../database/db.php';
include_once __DIR__ . '/person.php';
class Location { class Location {
public $id; public $id;
@ -19,8 +20,18 @@ class Locations {
$query = file_get_contents(__DIR__ . '/../database/sql/locations/find.sql'); $query = file_get_contents(__DIR__ . '/../database/sql/locations/find.sql');
$result = pg_query($query); $result = pg_query($query);
$locations = array(); $locations = array();
$current_location = null;
while($data = pg_fetch_object($result)){ while($data = pg_fetch_object($result)){
$locations[] = new Location(intval($data->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; return $locations;

Loading…
Cancel
Save