 Hey everybody, it's Brian and welcome to the eighth ye2 tutorial today We're going to be discussing the model class and to this point. It's pretty much just been theory We're gonna actually write some code here and just kind of get some hands on As you can see, there's a lot of subclasses of this thing Like active base active record, etc. Etc. I mean there's Model class. I cannot understate how important this is to the whole framework itself I mean, it's just ridiculous. I mean if you look at what all is in this thing I mean, it's just tons and tons of stuff. So What's super important is? Understanding what the model class is Why it exists and how to work with it and we've kind of gone over what it is I think we've actually beaten that to death, but once again a model is a representation of the data You've seen where we've gone and diggy and we've actually generated these model classes And you can see how you know, it's just gobbly cook at this point. We don't really know what it is So like let's look at categories here Categories is a pretty simple table. Let's actually Select this thing. He's just got a primary key in the name and in our model We can see it's an active record, which is an indicator that hey warning sign. This goes to the database and It's got some rules in here meaning this is how the model knows how to talk to the database It's all these rules really mean is the ID categories is required It's an integer. It's unique You know what just out of curiosity here. I want to try something. I actually If you followed up on a previous tutorials, I Wipe my computer hard drive and I had to start over Because I installed new version of Windows and everything. Yeah, I see there is a difference. So let me overwrite that All right now let the tutorial commence. All right So let's go back out here. Let's look at this bad boy again Okay, I Saw that stuff with the primary key and I thought now wait a minute. Why is that there and the reason why is because When I was in here, I forgot to flag. This is auto increment the first time I did this. I better double-check that real quick Okay, yeah, it's at auto increment. Otherwise, what would have happened is we'd have to fill in that primary key Which we obviously don't want to do so Lesson learned if you see the primary key and the rules you might want to go back out to key and regenerate just like I did All right, so We've got a table name. This tells us what table this interface is with We've got a rule set that tells us what we can and cannot modify and We've got attribute labels and these are displayed in forms like you got the ID categories and the names Etc. Now when I say displayed in a form What we're really talking about here is Here you can see these things these are the labels that would be in the pets class All right, so we're gonna work with this because it's very simple very easy. So we're gonna go out to our test route right here Ta-da, and we're gonna make it so that when we click that Some magic happens and some tape or some records are created in our little database here So let's go out here and we'll say All right now you notice the first thing does is does this fully qualified path And if you don't want that if you want the simpler way you just include this at the top of the file You can get rid of all that nonsense there. Just make it much easier And we're going to say model Name equal and we're going to say We're just gonna save this to the database. This is like the simplest thing in the world to do with a model So we're making a blank model We're setting the parameter of name and we're gonna say cats and then we're just gonna save it to the database now what this does is If it has a primary key set it will generate the update code But if there's no primary key like if it's set to null it'll actually generate the insert SQL code So that's the difference here. So you've seen on like let's go into Right here like model load This is for the create, right? It's gonna load it and then save update same thing model load is because in that post that primary key will be in There as a parameter So we're not messing around with any of that. We're just gonna focus on this So model equal new categories model name equal cats and then model save Make sure we save the file otherwise Dramatically nothing will happen here. All right. It doesn't look like much happened, but under the hood when we go here We have cats So let's go ahead and Let's make dogs Cuz I'm lazy. I'm just gonna copy and paste Model to whoopsie Dish that would have been funny So we're gonna have cats already in their dogs and fish, right? So let's click this And we're gonna go here and bang Go here refresh our database and we have cats dogs and fish Pretty simple pretty easy to understand Now what happens if we want to change that like instead of fish we want birds We're gonna say and sometimes IntelliSense works on this computer and sometimes it doesn't we're gonna go find one whoops When in doubt you can always kind of cheat and go down here and actually look at the code and find model They're just doing find one and then the ID. So let's go back up here and make sure we're doing this Yep, find one and the ID. What was the ID of that thing? Three so we're just gonna do that and we're gonna name fish to birds I'm not a big fan of fish that fish are just kind of weird and creepy and they just you know float around I'm gonna do that and then we're going to our database. I really got to move these icons around drives me crazy So watch fish when I refresh this this should flip to birds. Yep. There it is style birds Now what happens if you don't know the primary key and you still want to do this You go find tick tock come on IntelliSense It's because I've got all these other projects in here. That's why it does that. We'll see where There we go. This is what I was looking for here where condition and params. So we're gonna say where and Say name equal colon name And prams is actually an array. Oopsie seems kind of convoluted, but that's actually the syntax on how to do this I Hope otherwise, this is gonna be kind of embarrassing because yeah, syntax highlighting was still kind of screwing up on me So we're gonna rename birds to lizards So what we're doing here is we're saying categories find where name equal and notice this semicolon here Or see sorry the colon That colon denotes that it's a variable and the variable is going to be in this array. So we can name this ABC as long as it's named ABC over here. It'll still find it. That's called a query parameter. It's really all that is So we could have hard-coded it here But I like doing it over here that way you have your nice where statement and then you have your variables And then you can set your variables however you want them and then one because If you don't do the one function You could actually return multiple records and then you're gonna have a hard time because you'll be working with an array instead of a single record And because we're actually, you know pulling an object here. We should say if not Is no oops Not is no. I was thinking of a totally different language though than PHP if is set Come on Yeah, sometimes net bean Intellisense isn't the fastest thing in the world. I actually type faster than it does sometimes All right, so if it is not set, then we're going to say E2 oopsie warning Record not set Do this? All right, so really all we're doing here and we can actually get rid of that too because that's just gonna be annoying We're gonna say our model equals categories find where name is and then we've got a variable this variable is equal to Birds so we're essentially saying we're name equal birds and we want to find just one record instead of a bunch of them If the model is null meaning it hasn't been set then we're gonna issue a warning Otherwise, we're just gonna save it to lizards and click that and that should now say lizards. There it is So what other things can you do with models here? Well? Let's play around with one. Let's say wipe all that out. We're gonna do some validation here We're gonna do basic validation I think we're gonna be done with this tutorial cuz I'm a little beat long Intellisense No, okay fine be that way. You know, it's funny because I've got to like this monster lightning fast computer and Intellisense and net beans is just so slow Well, I shouldn't say that in net beans defense. I have multiple websites with multiple versions of ye So it's got a kind of Figure out where it is and what it's doing and how to do it and all that there we are So we're gonna make a new pets now if we look at pets pets is a much more complex class meaning it's got some things and One of those is mandatory So if we look at this you'll see the rules ID category required and why is that required? Remember how I said if you see a primary key watch out red flag That's because in our database. Let's we look at pets. We did a foreign key constraint So you'll see there's pets categories equals the category that I you know ID categories In case you totally fell asleep through in that tutorial basically we have this table called categories and We have a primary key called ID categories now in the pets table We have a key here called ID category. I should call it key an index and We are going to point the category Let's say lizards to this record right here So in the database, we'll see the number three button ye you can actually load that and get the name It's actually pretty slick the way it does it But to do that you need what's called a foreign key constraint So we've already created that in the database here. So that explains that craziness. So when you see this go Oh, something's going on here And then we've just got labels in here and Here's another telltale sign that we have a foreign key constraint. You see this get ID category zero This has one meaning. It's a one for one relationship. There will be one Category in this record. All right. So if that wasn't confusing, hopefully this will kind of confuse you less So we're gonna say model That I don't remember what's in the model here name description cost picture. Okay, so we're gonna say name Equal I need a good name. What is a good name Sparky? Why not name Sparky model Description whoops, I cannot spell big dog Now I'm going to purposely screw up here because I you know I Screw up quite a bit without doing it intentionally, but I'm gonna do this intentionally just to show you what happens So if we say model name model description and then model save and but you notice how this ID category is required I want to show you what happens. You know your code is just gonna explode. So Yeah, PHP compiler cannot use pets because pets Name is already in use Okay, I guess I did screw up on intentionally my bad Yeah, basically, it's because we're already using that namespace This will be the screw up that I actually wanted to show you hold on now You see how there's no error message Let's go out to our pets Now where is it? We hit save but there's nothing there. We can refresh all day long There's nothing there So let's go into ye and see exactly what happened. You see how in the logs Nothing really is going on here Model not inserted due to validation error. That's a telltale sign But you notice how it didn't actually throw an error message It didn't throw anything up on the screen. It didn't really give you any severe indication that something was wrong So model not inserted due to validation error. That's quite common error. So Let's see how we can fix this I'm sure you already know because we've talked about this, but I want to show you kind of a little trick here If model Through the magic of copy and paste We're gonna say if the model is validated meaning it's gonna go out It's gonna check every field and say are you what you need to be and If it is then we're gonna save it otherwise. We're gonna say ye whoops warning and Model we're gonna grab the errors The errors will be an array I just like throwing it out in the warning that way. It's just very, you know user-friendly easy to see here So we're gonna go back out here go back a dated. What what did I screw up here? I know I screwed something up validate derp That's why I always keep the API up handy And that's why I really love IntelliSense when it works So what we're saying is if model validate we're gonna validate that model then save it if we could not validate It's gonna jump down here. It's gonna throw our errors list out there. So let's try that again here All right, so you see we have a warning down in our log We can go in there and you see how a warning application and there it is ID category cannot be blank So that is how you would debug validation Through the debugger. There's a much more user-friendly way of doing that, but that's in a nutshell how we do it So let's go in here and we're gonna say Model I'm gonna cheat and I'm just gonna grab it and let's do this. Let's say dog Whoopsie And this should be kind of you know self-descriptive from what we did earlier here Whoops, I got a check and see if that was dog and not dogs At least my category. Yeah, it is dogs. Okay. All right, so And primary key will actually help you with a lot of things So you don't have to say ID categories or ID dogs Whatever you need me prior primary key you auto-detects with the primary key is so you can just say the model primary key We should probably put a little bit of logic in here just to you know save us a little bit That way if we don't actually have an object here We can just say you warnings There we go right so that looks a little more confusing But this is actually a little more professionally done quotey fingers around professionally What we're doing is we're grabbing the dog record and we're saying you know if it's not set then just issue a warning If it is set, however, we're gonna actually make a new dog called sparky. We're gonna set the ID categories to the dog Primary key Description big dog. We're gonna validate the model and then try to save it if it can't save we're gonna spit out our warning message here so let's go here and All right, let's go out to our database and see what that looks like pets Yeah, I actually only created two of them because I clicked it So as you can see it works and if we go out here and let's just click this like a dozen more times We'll have a bunch of dogs in their name sparky Yep, see there you go So that in a nutshell is how you would work with models I hope you found this educational entertaining and we're gonna do much more with models I just wanted this to be kind of like a model primer to kind of get your feet wet in case you're at home struggling with this You're like, how do I find this? How do I do that? I would I set the primary key? Let's see. What else should I talk about here? Oh, yeah Be sure to visit my website void realms.com for source code for this and other tutorials I've been very slow to get the source out for this But I wanted to point out two quick things the site's 100% run off your donations I don't even put advertisements on my videos And if you have questions comments concerns visit the void realms Facebook group There's a link to it right out on my website void realms comm under contact and there's like almost 400 of us programmers out there and fluent multiple languages. That's it. Thanks for watching