Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that our base application is up and running, we're ready to model our data. We know we’ll have a Course object and we also know that we’ll have a Review object. Each course will have possibly many reviews. You might recognize this type of model relationship as one to many. One course, many reviews.
You can download the database here. Or if you prefer, you can set up the database yourself. If you want to create the tables, you can use the following SQL commands:
CREATE TABLE "courses" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`title` TEXT NOT NULL,
`url` TEXT NOT NULL )
CREATE TABLE "reviews" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`course_id` INTEGER NOT NULL,
`rating` INTEGER NOT NULL,
`comment` BLOB NOT NULL )
Now that our base application is up and
running, we're ready to model our data.
0:00
We know we'll have a course object.
0:05
We also know that we'll
have a review object.
0:07
Each course will have
possibly many reviews.
0:11
You might recognize this type of
model relationship as one-to-many.
0:14
One course, many reviews,
let's build our models.
0:19
Once again, make sure that our containers
are closed with the semicolon.
0:24
Now we can close these documents for now.
0:30
Then we'll start by creating
a folder in the src directory.
0:36
We'll name this Model.
0:41
Our model's are going
to map to our database.
0:44
We'll start with our courses.
0:48
So we'll create a file named Course.
0:50
Course.php.
0:54
The first thing we need in
this file is our namespace.
0:59
This will tell our application
how to autoload this class.
1:04
This class will be in App\Model.
1:08
Then we can set class equal to Course.
1:13
In this course,
we'll have a protected $database property.
1:19
We'll accept this property as
a PDO variable on construct.
1:26
$this->database = $database;.
1:41
Next, we'll add our first
method to get all the courses.
1:47
To get our data from the database,
we'll need to start with a SQL statement.
1:57
$statement = $this->database->prepare.
2:03
SELECT * FROM courses, and
2:10
we'll ORDER BY id.
2:15
Then we run $statement->execute(),
2:22
And we can return,
$statement->, fetchAll().
2:29
Before we map anymore data,
let's get this working on the site.
2:38
We'll want to add our
course as a dependency, so
2:43
let's open our dependencies.
2:46
Once again, let's make some room and
set up a $container.
2:51
We'll call this course.
2:57
Let's make sure we close with a semicolon,
and
3:04
we'll return new App\Model\Course.
3:10
And we'll pass in, $c->get('db').
3:16
This is the dependency container we
set up above, which is our PDO object.
3:22
Now we can use this our routes.
3:30
Let's remove everything from
inside this route for now.
3:32
$result = $this->course->getCourses().
3:38
Since this is an API, we're going to
want to display the results as JSON,
3:48
so let's do that now.
3:54
return $response->withJson,
and we'll pass it the results.
3:56
WithJSON takes the data passed and
returns it as a JSON object.
4:04
It also send the appropriate
application slash JSON headers.
4:10
Optionally, we can pass a second
parameter, which is the status code.
4:15
By default, this is a status code of 200.
4:21
We're gonna want the status code of 200,
so we can pass that as well.
4:24
And the optional third parameter is for
encoding,
4:30
just like we would pass to
a json_encode function.
4:34
We can use JSON_PRETTY_PRINT.
4:38
Let's check this out in a browser.
4:44
Great, we have our three courses.
4:47
Let's get a single course.
4:51
You need to sign up for Treehouse in order to download course files.
Sign up