Okay, guys, now let's do some work on our app logic.
So let's divert a little from building the core features that we can then reuse on different other interesting
projects.
And instead let's add the models that are specific to our app.
First let me remove this exception from the home controller.
Okay, so we've got a tiny inconsistency in the folder naming.
So I went with views and with a controller.
So for the new folder let me call that models.
And we are going to fix this controller by just renaming it.
So let me make this controllers and then quickly fix that in the namespaces.
And also in the router where we are calling the action.
I just need to make sure the namespace is controllers.
Okay.
I think the inconsistency is fixed.
Let's go back to making some models.
So our app models will be classes extending from the base model class, giving us some powers to easily
work with specific database tables.
Let me begin by just creating the files, and I am going to create all three of them.
So we've got the user, we've got the post and we've got the comment one class for every table that
we have.
Okay.
So we can begin with the comment as why not?
Let's add a namespace in here, which would be app slash models.
And let's define the class.
That's the comment.
And that's important.
We need to extend the model and import it.
And the other things that we need to do at least at this point there will be more to do later on.
And we are gonna be going back to things I've explained earlier about dynamic properties.
Let me overwrite the table static property.
So now this class will know it needs to operate on the comments okay.
I can just copy paste this because it will save us some time.
So that's post and it operates on the posts table.
And this one is a user and this operates on users table.
Okay.
So I think it would be pretty fun to be able to just go ahead and play around with our new models.
I think we might be able to do that inside the home controller.
Let's use it for fun.
So first let me open the database.
It should be empty as we haven't added any records yet.
And we might try to add a new record inside the users.
Let's do it.
So we have this create method on the model to which we should be able to pass an array and create a
new database record.
Let's see if it works.
So I would like to create a user record here, or maybe even two.
I should be able to call the user create method and pass an array to it.
Now obviously we need to know what are the possible fields.
That's why I will jump to the database.
And the ID is automatic.
So is created at.
So it's automatically filled up by the database.
We should not explicitly pass the value for those columns but the name email password and role.
This is to be filled by us or.
So let's begin with the name.
Let it be my name.
The next column is email.
Let's come up with anything.
Then we've got the row.
Now this would be a string and we can customize it as we want.
I'm going to go with admin.
And finally the password.
So I'm leaving this as the last thing because this needs additional explanation.
As you should never really store passwords in plain text.
That's why we need to hash the password.
In PHP you've got a function for it.
It's called password hash.
You pass the password in plain text to this function.
So maybe this can be admin 123.
And next up you need to select an algorithm like Bcrypt or the default one.
I think the default one is bcrypt.
Anyway.
And this is everything.
That's everything that we need to create an account.
So let me create two accounts.
Let's make them a little bit different.
And let's also change the password.
Now next time I visit the home page this should just create those two users.
Now I'm not saying this makes sense to create the users automatically when we visit a page.
That's just a demo.
It's the quickest way we can test that.
Okay, so let's see if this create method works.
Um, maybe first I would need to start the server.
And I'm just going to refresh this page.
So there was no error, which means that probably everything went fine.
Why don't we just take a look?
So the database.
Let's jump to users.
Let me make this column a little bit smaller.
And there we have it.
There are two records.
Exactly the date that I've used with the role and created update.
So all the columns are populated, even those that we haven't explicitly specified.
And as you see, we've got a hashed password anyway.
This confirmed that our models work.
We tried the user model and the record was inserted into the users table.
So that's the end of this video.
But in the next one, I'd like us to create another command line script so that we don't have to insert
some fake data in random places.
So the thing is that to develop our app and to be able to constantly test it, at least manually, we
need some data.
It's best that we can get some fake generated data that we can insert with a CLI command.
So we don't have to do it manually, or just put the code in random places like we did right here.
So we're going to create this command next up.