$newPerson = new Person(null, $body_object->name, $body_object->age);
People::create($newPerson);
return '{"worked":true}';
People::create($newPerson); //pass $newPerson off to People, so it can add the data to the db
echo '{"worked":true}'; //return a success message
}
```
@ -126,9 +126,13 @@ Now, go to Postman and make a POST request to http://localhost:8888/people with
Check in `psql` to see if the person was created.
```SQL
SELECT * FROM people;
```
### Return data to the user
Currently, nothing comes back to the user after they create a new person. Let's change this to show all the users currently in the DB. Add the following to `models/person.php` for the `create()` method:
Currently, nothing helpful comes back to the user after they create a new person. Let's change this to show all the users currently in the DB. Add the following to `models/person.php` for the `create()` method:
The first difference you'll see is `([0-9]+)`. This is just more regex work. It basically means any integer. If you're interested in learning more about how this works, check out [these tutorials on regex](https://www.regular-expressions.info/tutorial.html). What it allows us to do is have a route for urls like `people/123`, `people/5`, or `people/2347346`, etc.
One that same line second line of the route, you'll notice `&id=$1` at the end of the rule. This adds a second query parameter to `controllers/people.php` called `id` and sets it to whatever is inside the `()` of `^people/([0-9]+)$`. In other words, if the URL is `people/2347346`. The id query param will be 2347346.
On that same second line of the route, you'll notice `&id=$1` at the end of the rule. This adds a second query parameter to `controllers/people.php` called `id` and sets it to whatever is inside the `()` of `^people/([0-9]+)$`. In other words, if the URL is `people/2347346`. The `id` query param will be 2347346.
Now let's update `controllers/people.php` to handle these requests. Add the following:
Now let's update `controllers/people.php` to handle these requests. Add the following at the end of your `if/else` section (note you'll have to remove an extra `}`):
```php
} else if ($_REQUEST['action'] === 'update'){
@ -202,6 +207,8 @@ Now let's update `controllers/people.php` to handle these requests. Add the fol
This is very similar to the create action. The only real difference is that we use `$_REQUEST['id']` to fetch the id of the person to be updated from the URL of the route. Everything else for the new `Person` object comes from the request body as normal.
Note that we're not actually creating a new `Person` object in the database, even though we have `$updatedPerson = new Person($_REQUEST['id'], $body_object->name, $body_object->age);`. Here, `$updatedPerson` is a new PHP object that resides in the computer's temporary memory, not in the DB. We're temporarily creating this PHP object so that we can pass it to `People::update()`, which will then use the properties of that PHP object to update an already pre-existing row in Postgres. Once we exit from the `else if` statement, `$updatedPerson` is destroyed in memory, since it is no longer needed.
## Delete
### Set up the model
@ -218,7 +225,7 @@ static function delete($id){
}
```
Note that the `$id` is just going to be an integer that we pass into `People:delete()`. Even if we only have one query param, we still need to put it in an array.
Note that the `$id` is just going to be an integer that we pass into `People:delete()`. In previous examples, it was an entire `Person` object, but we don't need that here. Just the id of the person to be deleted. Also, note that even if we only have one query param, we still need to put it in an array.
This is similar to update, but with the request method being DELETE and the `action` query param set to `delete`.
Now let's update `controllers/people.php` to handle these requests. Add the following:
Now let's update `controllers/people.php` to handle these requests. Add the following at the end of your `if/else` block (removing the extra `}` again):