From 37650301afc90fa8c29e44977417e14422153d0f Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 27 Jan 2019 11:19:57 -0500 Subject: [PATCH] models related --- controllers/locations.php | 16 ++++++------- controllers/people.php | 16 ++++++------- models/location.php | 47 ++++++++++++++++++++++++++++++--------- models/person.php | 31 +++++++++++++++++++++----- 4 files changed, 78 insertions(+), 32 deletions(-) diff --git a/controllers/locations.php b/controllers/locations.php index 272870e..6d15e3f 100644 --- a/controllers/locations.php +++ b/controllers/locations.php @@ -7,20 +7,20 @@ if($_REQUEST['action'] === 'index'){ } else if ($_REQUEST['action'] === 'post'){ $request_body = file_get_contents('php://input'); $body_object = json_decode($request_body); - $newLocation = new Location(null, $body_object->street, $body_object->city, $body_object->state); - $allLocations = Locations::create($newLocation); + $new_location = new Location(null, $body_object->street, $body_object->city, $body_object->state); + $all_locations = Locations::create($new_location); - echo json_encode($allLocations); + echo json_encode($all_locations); } else if ($_REQUEST['action'] === 'update'){ $request_body = file_get_contents('php://input'); $body_object = json_decode($request_body); - $updatedLocation = new Location($_REQUEST['id'], $body_object->street, $body_object->city, $body_object->state); - $allLocations = Locations::update($updatedLocation); + $updated_location = new Location($_REQUEST['id'], $body_object->street, $body_object->city, $body_object->state); + $all_locations = Locations::update($updated_location); - echo json_encode($allLocations); + echo json_encode($all_locations); } else if ($_REQUEST['action'] === 'delete'){ - $allLocations = Locations::delete($_REQUEST['id']); - echo json_encode($allLocations); + $all_locations = Locations::delete($_REQUEST['id']); + echo json_encode($all_locations); } ?> diff --git a/controllers/people.php b/controllers/people.php index 4954e4c..90d4b90 100644 --- a/controllers/people.php +++ b/controllers/people.php @@ -7,20 +7,20 @@ if($_REQUEST['action'] === 'index'){ } else if ($_REQUEST['action'] === 'post'){ $request_body = file_get_contents('php://input'); $body_object = json_decode($request_body); - $newPerson = new Person(null, $body_object->name, $body_object->age); - $allPeople = People::create($newPerson); + $new_person = new Person(null, $body_object->name, $body_object->age); + $all_people = People::create($new_person); - echo json_encode($allPeople); + echo json_encode($all_people); } else if ($_REQUEST['action'] === 'update'){ $request_body = file_get_contents('php://input'); $body_object = json_decode($request_body); - $updatedPerson = new Person($_REQUEST['id'], $body_object->name, $body_object->age); - $allPeople = People::update($updatedPerson); + $updated_person = new Person($_REQUEST['id'], $body_object->name, $body_object->age); + $all_people = People::update($updated_person); - echo json_encode($allPeople); + echo json_encode($all_people); } else if ($_REQUEST['action'] === 'delete'){ - $allPeople = People::delete($_REQUEST['id']); - echo json_encode($allPeople); + $all_people = People::delete($_REQUEST['id']); + echo json_encode($all_people); } ?> diff --git a/models/location.php b/models/location.php index 190f40a..248f00c 100644 --- a/models/location.php +++ b/models/location.php @@ -1,4 +1,5 @@ street = $street; $this->city = $city; $this->state = $state; + $this->inhabitants = []; } } class Locations { @@ -27,9 +29,9 @@ class Locations { return self::all(); } - static function update($updatedLocation){ + static function update($updated_location){ $query = "UPDATE locations SET street = $1, city = $2, state = $3 WHERE id = $4"; - $query_params = array($updatedLocation->street, $updatedLocation->city, $updatedLocation->state, $updatedLocation->id); + $query_params = array($updated_location->street, $updated_location->city, $updated_location->state, $updated_location->id); $result = pg_query_params($query, $query_params); return self::all(); @@ -37,18 +39,43 @@ class Locations { static function all(){ $locations = array(); - $results = pg_query("SELECT * FROM locations"); + $results = pg_query("SELECT + locations.*, + people.id AS person_id, + people.name, + people.age + FROM locations + LEFT JOIN people + ON locations.id = people.home_id + ORDER BY locations.id ASC"); $row_object = pg_fetch_object($results); + $last_location_id = null; while($row_object){ - $new_location = new Location( - intval($row_object->id), - $row_object->street, - $row_object->city, - $row_object->state - ); - $locations[] = $new_location; + if($row_object->id !== $last_location_id){ + $new_location = new Location( + $row_object->id, + $row_object->street, + $row_object->city, + $row_object->state + ); + $locations[] = $new_location; + $last_location_id = $row_object->id; + } + + if($row_object->person_id){ + $new_person = new Person( + intval($row_object->person_id), + $row_object->name, + $row_object->age + ); + + $locations_length = count($locations); + $last_index_of_locations = $locations_length-1; + $most_recently_added_location = $locations[$last_index_of_locations]; + $most_recently_added_location->inhabitants[] = $new_person; + } $row_object = pg_fetch_object($results); } diff --git a/models/person.php b/models/person.php index 6c54d02..8490b0c 100644 --- a/models/person.php +++ b/models/person.php @@ -1,4 +1,5 @@ name, $updatedPerson->age, $updatedPerson->id); + $query_params = array($updated_person->name, $updated_person->age, $updated_person->id); $result = pg_query_params($query, $query_params); return self::all(); @@ -35,16 +36,34 @@ class People { static function all(){ $people = array(); - $results = pg_query("SELECT * FROM people"); + $results = pg_query("SELECT + people.*, + locations.id AS location_id, + locations.street, + locations.city, + locations.state + FROM people + LEFT JOIN locations + ON people.home_id = locations.id;"); $row_object = pg_fetch_object($results); while($row_object){ - $new_person = new Person( - intval($row_object->id), + $row_object->id, $row_object->name, - intval($row_object->age) + $row_object->age ); + + if($row_object->location_id){ + $new_location = new Location( + intval($row_object->location_id), + $row_object->street, + $row_object->city, + $row_object->state + ); + $new_person->home = $new_location; + } + $people[] = $new_person; $row_object = pg_fetch_object($results);