diff --git a/controllers/people.php b/controllers/people.php index 67d4a94..520ea6c 100644 --- a/controllers/people.php +++ b/controllers/people.php @@ -7,7 +7,7 @@ if($_REQUEST['action'] === 'index'){ } else if ($_REQUEST['action'] === 'post'){ $requestBody = file_get_contents('php://input'); $body = json_decode($requestBody); - $newPerson = new Person(null, $body->name, $body->age); + $newPerson = new Person(null, $body->name, $body->age, $body->location_id); $allPeople = People::create($newPerson); diff --git a/database/sql/people/create.sql b/database/sql/people/create.sql index be4df42..0be4863 100644 --- a/database/sql/people/create.sql +++ b/database/sql/people/create.sql @@ -1 +1 @@ -INSERT INTO people (name, age) VALUES ($1, $2) +INSERT INTO people (name, age, location_id) VALUES ($1, $2, $3) diff --git a/models/person.php b/models/person.php index 45a15f8..a3b7f76 100644 --- a/models/person.php +++ b/models/person.php @@ -8,10 +8,13 @@ class Person { public $id; public $name; public $age; - public function __construct($id, $name, $age) { + public function __construct($id, $name, $age, $location_id = null) { $this->id = $id; $this->name = $name; $this->age = $age; + if($location_id){ + $this->location_id = $location_id; + } } } @@ -42,7 +45,7 @@ class People { } static function create($person){ $query = file_get_contents(__DIR__ . '/../database/sql/people/create.sql'); - $result = pg_query_params($query, array($person->name, $person->age)); + $result = pg_query_params($query, array($person->name, $person->age, $person->location_id)); return self::find(); }