WEBVTT

00:00.950 --> 00:05.540
Okay, guys, now let's do some work on our app logic.

00:05.540 --> 00:14.510
So let's divert a little from building the core features that we can then reuse on different other interesting

00:14.510 --> 00:15.380
projects.

00:15.380 --> 00:20.660
And instead let's add the models that are specific to our app.

00:20.660 --> 00:25.340
First let me remove this exception from the home controller.

00:26.600 --> 00:32.720
Okay, so we've got a tiny inconsistency in the folder naming.

00:32.810 --> 00:36.110
So I went with views and with a controller.

00:36.110 --> 00:40.280
So for the new folder let me call that models.

00:40.280 --> 00:45.650
And we are going to fix this controller by just renaming it.

00:46.160 --> 00:52.970
So let me make this controllers and then quickly fix that in the namespaces.

00:52.970 --> 01:00.590
And also in the router where we are calling the action.

01:00.590 --> 01:04.100
I just need to make sure the namespace is controllers.

01:04.340 --> 01:05.030
Okay.

01:05.070 --> 01:07.710
I think the inconsistency is fixed.

01:07.740 --> 01:10.260
Let's go back to making some models.

01:10.920 --> 01:21.000
So our app models will be classes extending from the base model class, giving us some powers to easily

01:21.000 --> 01:23.700
work with specific database tables.

01:24.000 --> 01:30.180
Let me begin by just creating the files, and I am going to create all three of them.

01:30.300 --> 01:39.240
So we've got the user, we've got the post and we've got the comment one class for every table that

01:39.240 --> 01:40.020
we have.

01:41.070 --> 01:41.310
Okay.

01:41.340 --> 01:44.760
So we can begin with the comment as why not?

01:45.120 --> 01:50.340
Let's add a namespace in here, which would be app slash models.

01:50.790 --> 01:52.950
And let's define the class.

01:52.950 --> 01:54.480
That's the comment.

01:54.480 --> 01:55.950
And that's important.

01:55.950 --> 01:59.400
We need to extend the model and import it.

01:59.670 --> 02:07.170
And the other things that we need to do at least at this point there will be more to do later on.

02:08.040 --> 02:14.130
And we are gonna be going back to things I've explained earlier about dynamic properties.

02:15.090 --> 02:21.360
Let me overwrite the table static property.

02:21.870 --> 02:27.930
So now this class will know it needs to operate on the comments okay.

02:27.960 --> 02:32.010
I can just copy paste this because it will save us some time.

02:32.010 --> 02:36.600
So that's post and it operates on the posts table.

02:36.900 --> 02:43.110
And this one is a user and this operates on users table.

02:43.680 --> 02:43.920
Okay.

02:43.950 --> 02:51.720
So I think it would be pretty fun to be able to just go ahead and play around with our new models.

02:52.590 --> 02:57.750
I think we might be able to do that inside the home controller.

02:57.810 --> 02:59.730
Let's use it for fun.

02:59.760 --> 03:03.240
So first let me open the database.

03:03.240 --> 03:07.080
It should be empty as we haven't added any records yet.

03:07.320 --> 03:12.900
And we might try to add a new record inside the users.

03:13.150 --> 03:14.470
Let's do it.

03:14.470 --> 03:23.650
So we have this create method on the model to which we should be able to pass an array and create a

03:23.650 --> 03:25.360
new database record.

03:25.780 --> 03:28.210
Let's see if it works.

03:29.560 --> 03:34.720
So I would like to create a user record here, or maybe even two.

03:35.530 --> 03:41.500
I should be able to call the user create method and pass an array to it.

03:41.530 --> 03:46.420
Now obviously we need to know what are the possible fields.

03:46.930 --> 03:49.960
That's why I will jump to the database.

03:49.960 --> 03:51.910
And the ID is automatic.

03:51.910 --> 03:53.770
So is created at.

03:53.920 --> 03:57.670
So it's automatically filled up by the database.

03:57.910 --> 04:06.340
We should not explicitly pass the value for those columns but the name email password and role.

04:06.370 --> 04:10.570
This is to be filled by us or.

04:13.420 --> 04:15.940
So let's begin with the name.

04:17.770 --> 04:18.910
Let it be my name.

04:18.910 --> 04:22.210
The next column is email.

04:22.840 --> 04:24.700
Let's come up with anything.

04:24.940 --> 04:27.040
Then we've got the row.

04:27.070 --> 04:31.120
Now this would be a string and we can customize it as we want.

04:31.150 --> 04:33.310
I'm going to go with admin.

04:33.610 --> 04:36.130
And finally the password.

04:36.130 --> 04:41.260
So I'm leaving this as the last thing because this needs additional explanation.

04:41.470 --> 04:45.970
As you should never really store passwords in plain text.

04:46.120 --> 04:48.580
That's why we need to hash the password.

04:48.610 --> 04:51.220
In PHP you've got a function for it.

04:51.220 --> 04:53.380
It's called password hash.

04:53.410 --> 04:58.600
You pass the password in plain text to this function.

04:59.200 --> 05:04.360
So maybe this can be admin 123.

05:04.480 --> 05:11.440
And next up you need to select an algorithm like Bcrypt or the default one.

05:11.440 --> 05:13.510
I think the default one is bcrypt.

05:13.510 --> 05:15.730
Anyway.

05:15.730 --> 05:18.760
And this is everything.

05:18.760 --> 05:22.190
That's everything that we need to create an account.

05:22.370 --> 05:24.740
So let me create two accounts.

05:25.460 --> 05:27.560
Let's make them a little bit different.

05:27.770 --> 05:31.130
And let's also change the password.

05:32.570 --> 05:37.550
Now next time I visit the home page this should just create those two users.

05:37.580 --> 05:42.710
Now I'm not saying this makes sense to create the users automatically when we visit a page.

05:42.890 --> 05:44.540
That's just a demo.

05:44.570 --> 05:47.090
It's the quickest way we can test that.

05:48.890 --> 05:51.770
Okay, so let's see if this create method works.

05:52.190 --> 05:57.740
Um, maybe first I would need to start the server.

05:58.700 --> 06:01.070
And I'm just going to refresh this page.

06:01.070 --> 06:06.290
So there was no error, which means that probably everything went fine.

06:07.490 --> 06:09.350
Why don't we just take a look?

06:09.740 --> 06:11.330
So the database.

06:11.330 --> 06:13.220
Let's jump to users.

06:13.940 --> 06:16.880
Let me make this column a little bit smaller.

06:17.000 --> 06:18.350
And there we have it.

06:18.380 --> 06:19.970
There are two records.

06:19.970 --> 06:25.010
Exactly the date that I've used with the role and created update.

06:25.580 --> 06:31.400
So all the columns are populated, even those that we haven't explicitly specified.

06:31.400 --> 06:35.870
And as you see, we've got a hashed password anyway.

06:36.350 --> 06:39.530
This confirmed that our models work.

06:39.950 --> 06:46.130
We tried the user model and the record was inserted into the users table.

06:46.400 --> 06:49.280
So that's the end of this video.

06:49.310 --> 06:56.780
But in the next one, I'd like us to create another command line script so that we don't have to insert

06:56.780 --> 07:00.500
some fake data in random places.

07:00.500 --> 07:09.200
So the thing is that to develop our app and to be able to constantly test it, at least manually, we

07:09.200 --> 07:10.400
need some data.

07:10.400 --> 07:19.070
It's best that we can get some fake generated data that we can insert with a CLI command.

07:19.070 --> 07:26.510
So we don't have to do it manually, or just put the code in random places like we did right here.

07:26.510 --> 07:30.200
So we're going to create this command next up.
