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'){
$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);
}
?>

@ -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);
}
?>

@ -1,4 +1,5 @@
<?php
include_once __DIR__ . '/person.php';
$dbconn = pg_connect("host=localhost dbname=contacts2");
class Location {
@ -11,6 +12,7 @@ class Location {
$this->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){
if($row_object->id !== $last_location_id){
$new_location = new Location(
intval($row_object->id),
$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);
}

@ -1,4 +1,5 @@
<?php
include_once __DIR__ . '/location.php';
$dbconn = pg_connect("host=localhost dbname=contacts2");
class Person {
@ -25,9 +26,9 @@ class People {
return self::all();
}
static function update($updatedPerson){
static function update($updated_person){
$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);
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);

Loading…
Cancel
Save