Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10

Video Transcription

00:00
I Welcome back to module four. This is less than 4.8 seeding the database one. And this lesson one to get started with seating the database, getting all that fake data into our development database so that we can
00:11
test much better than if we're trying to manually insert a record here and there.
00:16
So what we're gonna do, we're gonna insert a lot of fake data into our database is gonna be much better for testing. It's gonna be much faster. We can easily dump and regenerate data we can use Tinker to test faker calls. Faker is a library that provides us with a bunch of fake data so that we don't have to write functions that would generate fake data for us.
00:36
Ah, faker and method at calls can be found at the following u r l Now with that said, Let's get started. I Welcome back. Now let's get started with seating the database. I've gotten a head start that's so that we can fit this into two videos. But don't worry. We're going to walk through everything and explain every line of code
00:55
that's going on here.
00:57
So I have the database cedar dot PHP File open.
01:02
That's in the shared Database Seeds directory. And it's likely that when you open this file that you will only have this line in that file within your run function. And that's this line that is commented out with these two Ford slashes. And what that means is that it's not doing anything.
01:21
It's just there is an example.
01:23
It's it's a comment.
01:26
So when you run the database cedar, the primary thing that's gonna happen is that this function is going to run.
01:34
So the first thing that happens in this function is we have a DB statement set foreign key checks to zero, and that is so that our database does not complain when we delete all our data, we otherwise, when we try to truncate the tables in order to make new data,
01:51
the database would complain and say, You can truncate this data. It's related to other data.
01:57
So to alleviate that, we set this to zero, which is false, which means it's not occurring in the database. We then truncate
02:06
our database tables because I've done room in building. I'm truncating those tables in these two commands.
02:15
The next thing that occurs is we make a factory
02:17
command call and we pass it to building class and we ask you to create 20 of those.
02:24
And that's what this line does.
02:28
Now, once we start inserting data that is related to other data or has foreign key constraints, we need to use this concurrency function, which is this function down here.
02:38
So what this is going to do this is gonna pass the room class
02:43
as the class in the function and then do a factory create call on it one by one. So if we send it 100 like I am here, it's gonna go into afford loop
02:53
and cycle 100 times to effectively create 100 rooms. Now, the reason we do that is because if we take a look at the room factory,
03:02
we are getting in random order, order a building
03:07
and the first i d.
03:09
And then we're using that to create the model. So in the in this example, we can see that we were turning in name
03:16
that we use a
03:19
A class called faker to create a unique user name
03:23
again, we use figure to create a unique number between 25 100. And then we also say that building I d is going to be that random building that we generated at the top here. Now, if we did this the traditional way, which is just using this command,
03:39
what it would do is it would break the call into a bunch of threads and a bunch of depending on the computer. A bunch of
03:49
your
03:51
rooms would have the exact same building number, so it won't be truly in random order. You get a bunch of duplicates
04:00
and then the next item is that we set foreign key checks, the one so we turn our foreign key checks back on so that it works again.
04:08
So let's go ahead and run this and see what that looks like.
04:13
So here we are. I have a shell session into my virtual machine in Bagram.
04:17
Come and do a PHP partition DBC,
04:25
and we can see that it's completed successfully. So let's see what that looks like in the database.
04:30
So here are the rooms
04:32
me refresh. Um,
04:34
they changed again because I just receded it.
04:36
So this is all our random room data, and they belong to real buildings Because we have the foreign key here. We can see the different building names.
04:46
So I go looking buildings again. We have those 20 random buildings.
04:50
So we've generated random data for our buildings and our rooms.
04:55
Let's go ahead and start generating random data for our next table,
05:00
which looks like it should be.
05:03
Look at my migrations.
05:05
Course is Okay,
05:09
so I'm going to go back into my shell such session, and I'm gonna do a I'm going up to the command that I used before.
05:18
So this is a command. It's PHP are Titian make factory.
05:23
And this is the previous one room factory Dash him room and dash absence were model. What? We are doing the room right now, So I'm gonna call this one the course factory.
05:34
I could name it anything I wanted, but I'm gonna name it something that makes sense. It's for the model. Of course.
05:42
I would hit in her
05:45
and we go factory created. So what the dad do for us? Well, if we go into our folder structure, I'm going to the database and we look in the factories folder.
05:53
We'll see a course factory. Now, if I go ahead and open this.
05:58
We'll see that it's pretty much empty,
06:00
so we have to start adding that data.
06:02
So let's go look at our courses
06:06
and see how many columns we have. This one actually has quite a few.
06:12
So will be a good example. So
06:15
we don't have to worry about the 1st 1 The i d. It's an auto incriminating field, so room I d
06:21
and I'm gonna leave these blank for now.
06:26
So we got a quote it to
06:30
so we got to get the syntax right. We have to have the the quotes
06:39
you got that the commas. If you know, if we missed that, then of course it will fail and complain about it.
06:50
Andi
06:54
Max Students
07:00
Onda started it
07:10
on and it
07:14
and we don't have to handle the
07:16
created at updated at or deleted at lower levels. Handles that by itself. Then, of course, will be dealing with that in the sequel code later on.
07:28
So I've these are all empty right now.
07:31
So a good idea
07:34
is to
07:36
PHP partition,
07:40
tinker
07:43
and spell it right
07:48
on. The commands we need is we need
07:51
use faker,
07:55
and we need to just set the variable faker equal to a new instance of the faker with this command and it's common to have to look this kind of stuff up.
08:05
So now we have a faker
08:07
and since so I can type in this
08:09
faker arrow name
08:13
and I can see that what values I'm going to get this will let me test things.
08:18
So we have a course code.
08:22
It's gonna the faker website and see what kind of items we have to choose from.
08:26
So let's just search for code first. So postcode
08:31
country code
08:33
currency code.
08:35
No, we don't want this. We want to stuff at the top.
08:39
Okay,
08:41
so it looks like we aren't going to get a, um,
08:45
good coding,
08:46
Uh, example. So we'll just use a building number,
08:52
so I I contest this so I can put in building
08:56
number.
08:58
Can I could see that I'm gonna get building numbers
09:01
now. What I'm also gonna need to do is type in the word unique
09:05
on a narrow.
09:07
And what this does is this is will ensure I don't get the same building number or code because we have a unique constraint
09:16
on that course code.
09:18
So we want to make sure that we don't use the same one more than once because the database will complain and crash.
09:24
So there is one example title.
09:28
Let's see if we just do faker title what we get
09:33
That's not what we want
09:35
about job.
09:39
So title
09:41
that's close enough for me.
09:43
So
09:46
what we'll do is work.
09:48
Actually, I could just type that in
09:56
on. We'll do unique on this one as well.
10:03
Okay, Max. Students
10:07
gonna look over at what I used over here.
10:11
It's gonna copy this from my room factory
10:16
on. I'm just gonna leave the same
10:20
now, the start date
10:26
for the start in end date. We're going to use the date time between, and we're gonna adjust it so that they make sense.
10:35
Let me go ahead and copy this.
10:46
I'm gonna paste it,
10:48
go ahead and make it whiter.
10:50
So let's say
10:54
plus one year
10:56
and the end eight equals,
11:03
actually, let's change this. Let's change this one too. Now,
11:07
on the end date for this one year.
11:11
So from now to one year,
11:16
and
11:18
just to make sure that the end A doesn't start before the
11:22
start date which wouldn't make a lot of sense. We're going to make sure that this ends in the future.
11:30
So we're going to one year
11:33
and not These are quotes.
11:37
And then for this one, we're gonna do two years.
11:41
Okay,
11:43
so now we need our
11:46
room, i d.
11:48
So we're gonna need to run role a random room. And we did that in the room.
11:54
So we're gonna
11:58
so we're gonna do room
12:03
equals room capital are on that one in random, or everything else is fine.
12:09
And then we'll drop this down into that 1st 1
12:13
Now, we're still missing something we need to use.
12:16
Yeah,
12:18
room. We have to have that resource in this file. Otherwise, this file will not know what a room is. It will complain when it gets to this line. By including this line of code which says you zap room, it becomes aware of what a room is. So that's an important piece right there.
12:37
So when working with these files, of course, what you want to do is make sure that you're getting the syntax correct. Make sure you have your semicolons, your commas,
12:45
your quotes
12:46
in the appropriate places. Because violating the syntax will lead to a crash when you go to see
12:52
Okay, So I think we're ready to run the course. Let's go ahead and put it here. We need to do a concurrency.
13:03
And this, of course, is gonna be course.
13:07
Now that we've included course in a file, what do we need to dio? We need to make sure this file knows what that ISS. So we need to use APP course
13:18
with a Capital C.
13:22
And also let's truncate the course
13:26
this way when we run the cedar again,
13:30
we won't just keep adding two courses, which can cause failures when we're using unique in the faker.
13:35
Because the faker will not realize that we already have that name out there or that code out there.
13:41
So let's go ahead and run this and
13:45
check that it works.
13:48
I know this lessons getting along,
13:54
so it's gonna be PHP are Titian
13:58
D B colon seed.
14:07
Okay,
14:09
men student doesn't have it. Default Follow you.
14:15
Okay. The men students
14:18
Did we put that in there? We have Max students. We don't have men students.
14:24
We will just
14:28
at it like so
14:31
I will say one
14:33
to 10
14:35
save
14:37
Ron R cedar again.
14:46
It is successful. Let's check D Beaver.
14:52
So we did courses, data
14:56
refresh.
14:58
There are all the courses now
15:01
with all our random data. So we're on our way.
15:07
So this course is getting long on the long side, and I apologize for that. And it does have quite a bit of information in it that we went over. Let's go ahead and head over to the summary, and then we'll move on to the next course from there.
15:24
So that brings us to the 4.8 seeding the database. One summary realized this lesson was getting a little bit long, but there was a lot of good information in there. We really got started with seating our database and the next couple of lessons we're gonna finish. We're going all the way to the end and make sure that our whole database gets seated.
15:41
So in this lesson, we reviewed the building and room factories that I got a head start on. And then we created the course factory, and the next lesson we will create even more factories. Oh, and I hope to see you there. Thank you.

Up Next

Intermediate SQL

This free course introduces the student to intermediate concepts found in the implementation and application of Structured Query Language (SQL) within professional business environments.

Instructed By

Instructor Profile Image
Kitt Parker
Instructor