models related

master
Matt Huntington 7 years ago
parent dd044ce2ca
commit 37650301af

@ -7,20 +7,20 @@ if($_REQUEST['action'] === 'index'){
} else if ($_REQUEST['action'] === 'post'){ } else if ($_REQUEST['action'] === 'post'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$body_object = json_decode($request_body); $body_object = json_decode($request_body);
$newLocation = new Location(null, $body_object->street, $body_object->city, $body_object->state); $new_location = new Location(null, $body_object->street, $body_object->city, $body_object->state);
$allLocations = Locations::create($newLocation); $all_locations = Locations::create($new_location);
echo json_encode($allLocations); echo json_encode($all_locations);
} else if ($_REQUEST['action'] === 'update'){ } else if ($_REQUEST['action'] === 'update'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$body_object = json_decode($request_body); $body_object = json_decode($request_body);
$updatedLocation = new Location($_REQUEST['id'], $body_object->street, $body_object->city, $body_object->state); $updated_location = new Location($_REQUEST['id'], $body_object->street, $body_object->city, $body_object->state);
$allLocations = Locations::update($updatedLocation); $all_locations = Locations::update($updated_location);
echo json_encode($allLocations); echo json_encode($all_locations);
} else if ($_REQUEST['action'] === 'delete'){ } else if ($_REQUEST['action'] === 'delete'){
$allLocations = Locations::delete($_REQUEST['id']); $all_locations = Locations::delete($_REQUEST['id']);
echo json_encode($allLocations); echo json_encode($all_locations);
} }
?> ?>

@ -7,20 +7,20 @@ if($_REQUEST['action'] === 'index'){
} else if ($_REQUEST['action'] === 'post'){ } else if ($_REQUEST['action'] === 'post'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$body_object = json_decode($request_body); $body_object = json_decode($request_body);
$newPerson = new Person(null, $body_object->name, $body_object->age); $new_person = new Person(null, $body_object->name, $body_object->age);
$allPeople = People::create($newPerson); $all_people = People::create($new_person);
echo json_encode($allPeople); echo json_encode($all_people);
} else if ($_REQUEST['action'] === 'update'){ } else if ($_REQUEST['action'] === 'update'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$body_object = json_decode($request_body); $body_object = json_decode($request_body);
$updatedPerson = new Person($_REQUEST['id'], $body_object->name, $body_object->age); $updated_person = new Person($_REQUEST['id'], $body_object->name, $body_object->age);
$allPeople = People::update($updatedPerson); $all_people = People::update($updated_person);
echo json_encode($allPeople); echo json_encode($all_people);
} else if ($_REQUEST['action'] === 'delete'){ } else if ($_REQUEST['action'] === 'delete'){
$allPeople = People::delete($_REQUEST['id']); $all_people = People::delete($_REQUEST['id']);
echo json_encode($allPeople); echo json_encode($all_people);
} }
?> ?>

@ -1,4 +1,5 @@
<?php <?php
include_once __DIR__ . '/person.php';
$dbconn = pg_connect("host=localhost dbname=contacts2"); $dbconn = pg_connect("host=localhost dbname=contacts2");
class Location { class Location {
@ -11,6 +12,7 @@ class Location {
$this->street = $street; $this->street = $street;
$this->city = $city; $this->city = $city;
$this->state = $state; $this->state = $state;
$this->inhabitants = [];
} }
} }
class Locations { class Locations {
@ -27,9 +29,9 @@ class Locations {
return self::all(); 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 = "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); $result = pg_query_params($query, $query_params);
return self::all(); return self::all();
@ -37,18 +39,43 @@ class Locations {
static function all(){ static function all(){
$locations = array(); $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); $row_object = pg_fetch_object($results);
$last_location_id = null;
while($row_object){ while($row_object){
$new_location = new Location( if($row_object->id !== $last_location_id){
intval($row_object->id), $new_location = new Location(
$row_object->street, $row_object->id,
$row_object->city, $row_object->street,
$row_object->state $row_object->city,
); $row_object->state
$locations[] = $new_location; );
$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); $row_object = pg_fetch_object($results);
} }

@ -1,4 +1,5 @@
<?php <?php
include_once __DIR__ . '/location.php';
$dbconn = pg_connect("host=localhost dbname=contacts2"); $dbconn = pg_connect("host=localhost dbname=contacts2");
class Person { class Person {
@ -25,9 +26,9 @@ class People {
return self::all(); return self::all();
} }
static function update($updatedPerson){ static function update($updated_person){
$query = "UPDATE people SET name = $1, age = $2 WHERE id = $3"; $query = "UPDATE people SET name = $1, age = $2 WHERE id = $3";
$query_params = array($updatedPerson->name, $updatedPerson->age, $updatedPerson->id); $query_params = array($updated_person->name, $updated_person->age, $updated_person->id);
$result = pg_query_params($query, $query_params); $result = pg_query_params($query, $query_params);
return self::all(); return self::all();
@ -35,16 +36,34 @@ class People {
static function all(){ static function all(){
$people = array(); $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); $row_object = pg_fetch_object($results);
while($row_object){ while($row_object){
$new_person = new Person( $new_person = new Person(
intval($row_object->id), $row_object->id,
$row_object->name, $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; $people[] = $new_person;
$row_object = pg_fetch_object($results); $row_object = pg_fetch_object($results);

Loading…
Cancel
Save