9 hours 41 minutes
Hi. Welcome to Module four. This is less than four point to implement models. One,
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
implement a teacher and a student
table as well as a user table, because we would have had three
different ideas that are kind of related to each other.
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
by the tape.
So the type table indicates the type
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.
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
or they could be a teacher. So the user
takes on many forms.
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.
And we'd make a requirement of where type equal students. And then we would get all the students.
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
talk about that as well.
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
a one to many on the grades table, so
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.
So when we connect a user
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.
So this is our most complicated relationship in this
database. Thus far, just additional relationship that we have out here is the room.
So we have the room
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
how you feel about the design. Now there are multiple ways we could have went with this.
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
But I think we could simplify it
just by having a type indicated on our pivot.
Now, with that said, Let's get into our virtual machine and create some of these models.
Okay, here we are at our machine. The first thing I'm going to do is to create a new database
Merkel education, because that's the, uh,
the role of this database.
So I've created the education database. Next,
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
So you want to make sure that in your database Migrations folder you just have the failed jobs table migration.
Then in your app, you want to make sure that there's no dot PHP models here.
And once you have that and you have the education databases, one more thing we need to do
we need to go into the settings
the dot m.
We need to edit that to use
the education database.
And next, let's start making our models
so we'll start with the user table.
So we need to
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.
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
next. I'm gonna hit up and I'm gonna go ahead and make the grade
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
to the name itself
next, our pivot table user course.
I actually can't just type it out. We need to make the model.
Okay, let's take a look at what we're missing.
We still need the course
and the room. Okay.
And lastly, let's make the room model.
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
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.
So let's go ahead and start adding some fields to our databases before we run the migration.
So we need to go into the database migrations.
We'll start with the users table,
and I know we're gonna need the names, so we're gonna do a first name and the last name field.
That's a type string.
that I'm going to copy this and make it the
Then we also need the phone number. So we're gonna have a phone number field
on. Then we're going to have a
We're gonna need a couple fields to facilitate the address.
It's tempting to make it Teoh one field, but you'll usually have an easier time if you break everything out.
And some databases, though, actually make an address table
where you would, uh,
could actually have the same address.
Which makes sense. In some cases,
we're just gonna keep it easy and leave those all those strings.
Let's go ahead and move on to the grades table. So I go ahead and open that
the one thing that's not
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.
So But we need a big imager,
and we're gonna call that
student or actually user I d.
And then we also need a another big energy
call. That course I d
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.
We'll just call it a letter grade,
and then we're gonna make another column.
We're gonna make this one
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.
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,
so But I opened up my terminal
we need the word migrate
Okay. Looks like it was successful. Let's take a look in D Beaver.
It should be going to the education database.
We need to refresh,
and it looks like we have them.
It looks like something went wrong with my users table. Take a look. Maybe I forgot to save.
Let's take a look at the other one. We did, which was grades.
My grades were there, so I probably forgot to save that file. Let's go ahead and
So if I go back to this Yep,
and I run the same command fresh.
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.
So now if I go back to the education and refresh,
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.
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.
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.
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.
And the next lesson we're gonna finish adding columns, Stewart tables.
And that completes this. Listen, so I hope to see in the next thank you.