Implement Models Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10
Video Transcription
00:00
Hi. Welcome to Module four. This is less than four point to implement models. One,
00:05
there is an E R D diagram provided. We're also gonna look at it in this lesson, but feel free to go ahead and download it and bring it up so you can look at it as we go through these bullet points. I think it might help, as we describe what is on that diagram. Some diagram notes includes the fact that we only have one user table. We did not
00:24
implement a teacher and a student
00:26
table as well as a user table, because we would have had three
00:32
different ideas that are kind of related to each other.
00:35
And we There's a way for us to put that all into one table and simple fire design. And that is by having a user's role indicated in the pivot table
00:45
by the tape.
00:47
So the type table indicates the type
00:49
off the user, whether they are a student or a teacher or what role they're playing in the course and the pivot table in question or the one that's on the RD diagram is the user underscore course table.
01:03
This is a more advanced pivot table. The type is in the pivot, and this represents a polymorphic concept. And again, polymorphic just means many forms. What concept is taking many forms in our diagram? Whilst the user the user could be just a user, they could be a student
01:23
or they could be a teacher. So the user
01:25
takes on many forms.
01:26
Now with the er de diagram, as it's currently laid out, Weaken right views inquiries that expose key concepts. For example, let's say we want to query out all our students. All we would need to do is join the user table to the pivot table, which would also join to the type table.
01:44
And we'd make a requirement of where type equal students. And then we would get all the students.
01:49
We could do the same for teachers. And if we provide enough information on our course table, we could also, uh, quarry out current students or previous students. I do a lot of neat stuff with the diagram as it's laid out. That's it. Let's take a look at the R D diagram that thes bullet points are describing end
02:07
talk about that as well.
02:09
So this is the er de diagram that I came up based on the requirements in the document, and we don't have any of the columns in there. We have the relationships in there to really just expose how these tables are related to each other so we can see that the user can have
02:28
a one to many on the grades table, so
02:30
a student could have many grades. That makes sense. We see this pivot table in the center. Here we have the type. We have the user. We have the course.
02:43
So when we connect a user
02:46
in this table, we will have that user and the type of student. We will know that that user is a student and we'll have the course I d. So we'll know what course that student is in.
02:57
So this is our most complicated relationship in this
03:00
database. Thus far, just additional relationship that we have out here is the room.
03:07
So we have the room
03:08
that's related to the course to show what room the courses in and one of the fields that will be on that is the building. So we haven't missed any information because we have not exposed the columns in these tables, just the relationships and the CRD diagram is available for download, so feel free to download it. Take a look at it and see
03:25
how you feel about the design. Now there are multiple ways we could have went with this.
03:30
We didn't have to put the type on the pivot table. I just felt that it was the more efficient way to do it you could of, and you probably would run into some designers that make that might say, OK, I have a teacher's table, I have a student's table and I have a user's table
03:46
But I think we could simplify it
03:49
just by having a type indicated on our pivot.
03:53
Now, with that said, Let's get into our virtual machine and create some of these models.
03:58
Okay, here we are at our machine. The first thing I'm going to do is to create a new database
04:03
Merkel education, because that's the, uh,
04:08
the role of this database.
04:11
So I've created the education database. Next,
04:14
the next thing we need to do is make sure that we've removed any other of the previous lesson data that we might have implemented in our application. Currently
04:23
So you want to make sure that in your database Migrations folder you just have the failed jobs table migration.
04:31
Then in your app, you want to make sure that there's no dot PHP models here.
04:38
And once you have that and you have the education databases, one more thing we need to do
04:43
we need to go into the settings
04:45
the dot m.
04:46
We need to edit that to use
04:51
the education database.
04:55
Save that.
04:59
And next, let's start making our models
05:01
so we'll start with the user table.
05:04
So we need to
05:06
vagrant.
05:08
Ssh! It's gonna open up a shell session into our virtual machine so that we can issue those PHP are Titian commands against Ah, the machine. Now those PHP are Titian commands will not work on your host, so if you try to issue them there, they will fail.
05:27
So I'm gonna hit up to find the previous command that I had, which was a PHP are Titian make model bulk migration. But I'm gonna edit it. I will make this user
05:41
hit enter
05:44
next. I'm gonna hit up and I'm gonna go ahead and make the grade
05:54
next. I'm gonna hit up and I'm gonna make the user type. Now, note here is I'm not gonna put an underscore in the name. I'm just gonna sit hit user type levels actually going to add an underscore
06:05
to the name itself
06:10
next, our pivot table user course.
06:14
I actually can't just type it out. We need to make the model.
06:24
Okay, let's take a look at what we're missing.
06:28
We still need the course
06:30
and the room. Okay.
06:42
And lastly, let's make the room model.
06:53
So if you made a mistake and you spelled one of the names wrong and hit entered, all you would need to do is just
07:00
remake the model with the correct name and delete the file that get created by mistake, which would be a PHP file and the APP folder and immigration file in the database migration folder.
07:14
So let's go ahead and start adding some fields to our databases before we run the migration.
07:23
So we need to go into the database migrations.
07:29
We'll start with the users table,
07:32
and I know we're gonna need the names, so we're gonna do a first name and the last name field.
07:39
That's a type string.
07:44
First name
07:47
that I'm going to copy this and make it the
07:50
last name.
07:57
Then we also need the phone number. So we're gonna have a phone number field
08:07
on. Then we're going to have a
08:09
address field.
08:16
We're gonna need a couple fields to facilitate the address.
08:20
It's tempting to make it Teoh one field, but you'll usually have an easier time if you break everything out.
08:28
And some databases, though, actually make an address table
08:31
where you would, uh,
08:35
for users
08:37
could actually have the same address.
08:41
Which makes sense. In some cases,
08:52
we're just gonna keep it easy and leave those all those strings.
08:56
Let's go ahead and move on to the grades table. So I go ahead and open that
09:03
and
09:05
the one thing that's not
09:07
indicated on that is the course I d. We're gonna need a relationship back to the course because we will want to know what course the greatest four.
09:16
So But we need a big imager,
09:24
and we're gonna call that
09:26
student or actually user I d.
09:33
And then we also need a another big energy
09:41
call. That course I d
09:46
we're going to add to fields for the grade, so we're gonna say the 1st 1 is from Keep it easy again. Leave it as a string.
09:54
We'll just call it a letter grade,
10:01
and then we're gonna make another column.
10:05
We're gonna make this one
10:09
a decimal.
10:11
Call it
10:13
grade score.
10:20
And for the grade score, we're gonna give it a length of eight. Could be ate numbers long and precision of to sew up to two decimal places.
10:31
And that will handle that. Let's go ahead and run this migration and make sure we haven't made any mistakes thus far. That would cause a crash,
10:39
so But I opened up my terminal
10:43
make.
10:46
Actually,
10:48
we need the word migrate
10:50
fresh.
10:54
Okay. Looks like it was successful. Let's take a look in D Beaver.
11:00
It should be going to the education database.
11:03
We need to refresh,
11:07
and it looks like we have them.
11:09
It looks like something went wrong with my users table. Take a look. Maybe I forgot to save.
11:13
Let's take a look at the other one. We did, which was grades.
11:18
My grades were there, so I probably forgot to save that file. Let's go ahead and
11:22
fix that.
11:24
So if I go back to this Yep,
11:26
Save
11:28
and I run the same command fresh.
11:31
It's actually drop all the tables and Reince Stan. She ate them, so it literally deleted the whole database schema and then rebuilt it.
11:39
So now if I go back to the education and refresh,
11:45
I'll see all the fields for my user table. So that's all the modeling we're going to do right now. In this lesson, gonna move on to the summary for this lesson, and then in the next lesson, we're gonna finish the modelling process. So that brings us to four point to implement Model one summary.
12:01
So what did we do in this lesson? We reviewed some database concepts. We also discussed trying to limit the number of tables and use.
12:07
We avoided using a user table teacher table and a student table. Instead, we used one user table with a pivot table that indicates the users role in a course so that users could be a student or a teacher, as indicated by a role that's available and to find on another table.
12:26
We also reviewed the ER de diagram that was used to represent the database, and we implemented models in the level framework using R P HPR, Titian commands to create migration files and model files. We added some columns to a couple of those tables the user table and the great table.
12:43
And the next lesson we're gonna finish adding columns, Stewart tables.
12:46
And that completes this. Listen, so I hope to see in the next thank you.
Up Next