 Hey everybody, it's Brian and this is the 12th ye2t video. This is going to be, I want to say, the last of the model videos for a while. We're going to be talking about mixing models. So if we go to our site here and we go to Pets, you see how we have this create Pets and we've gone in and we've done this before. Now when we look at this, if we just hit create, you know, that's how bink ID category cannot be blank, but everything else can be. Well, it's kind of stupid to create, you know, a blank name with a blank description cost, etc, etc. So what we're going to do is we're going to mix models. Let's jump into my SQL here. And when we look at our table, you can see how, sure enough, ID category is not null. Everything else can be null. Well, it's very easy just to check this box, hit apply, and then go into G and update your model and blah, blah, blah. Sometimes you don't have that luxury. Sometimes somebody else builds the database and you're stuck doing the front end. And for whatever reason, Bob in the database department will not set that to not null. So in this video, we're going to cover how would you actually override this? So we're going to open up NetBeans and let's go to our models here. And you see our Pets model has this nifty little thing. Now there's one of two ways of doing this. The first way is you actually inherit Pets and then set the name and description to required. The other way, which we're going to be talking about is when you create a separate model all together. I just bumped the microphone, I think I did. You create a separate model all together and you load both those models at the same time. And this is actually very handy. So like let's go into Pets and let's look and see what's going on here. We've got our index, which just has this grid view that we've seen. Let's actually go out there and look at it here. So here's this nice little grid view. Now if we click create, you see how it's Pets slash create and it's showing this nice little form here. So let's go in and see what happens. We go to create and sure enough there is a this render the underscore form, which is up here. So in create and update, we're both going to, let's actually open update. You can see how both of them are almost identical. All they're doing is they're rendering this form. So this form is what we really got a mess with here. Now when you look at this, you can see how you have the variable model equal app models Pets. So we're going to go back to our Pets model and sure enough it is not required for name and description. So we're going to actually create our own model here. My Pets. Why not? Now let's call it required, required, required name. And we should probably call it required name form. You know what? Let's just call it form, required form. There we go. And the fastest way to do this is to actually grab an existing one like the login form that we previously done. We're going to copy that. We're going to just paste it in there and we'll get rid of that description here. And let's get rid of all this gobbly gook. So there's really not a whole lot to this. We're just going to fill this out a little bit. If I can type, I've got spaghetti fingers as they call it. I don't know why it's called spaghetti fingers. So a buddy of mine said that one time. I just never got that out of my head. Spaghetti fingers. Anyways. So very, very simple model. I mean you can do this in like two seconds. The only real rule is we have two variables, name, description, and they're both required. Now you may be asking, why wouldn't you just inherit pets? Well, there's really no reason not to other than I want to reuse this in other models. So we only have name, description. So let's say we have a, you know, categories also has name and description. We can reuse that model in categories in just about anything. Well let's close a bunch of this garbage out here and we're going to say, we'll call this recreq required form. I'm a little sore. I've been getting back to the gym and I tweaked my elbow. It's not very fun. All right. So we've got a variable called rec, but we're not really there yet. We have to, you guessed it, go to the controller. So let's go into create and update. Here's create. And we want to, so we're just going to create a new one of those. Now we want to actually look at this code and figure out what's going on. What we're saying here is if the model was loaded, what does that mean, loaded? It means they've gone out and they've clicked the button on the form. They've actually clicked this and we're loading the model. And then we're going to do that and that and model save. So we want to kind of change this around a little bit here. What we want to do is we want to say, let's grab that, and we're going to cut that out of there. So what we're going to do is we're going to say if the model and the other model, we called rec, which is short for required form. So if model and rec both loaded, then send it to view. But we want to do a little bit extra here because we're not loading those parameters on the model because we're using a different model in the form. It'll be very obvious here in a second. We have to actually update those manually. Whoopsie. All right, so we're going to load those, see if we can get a little more screen real estate here. That's a little better. Then we're going to, let's see here, and we're going to throw an error here. Let's just grab an error message. We'll throw a not found. Why not? My cat is over here making like the weirdest noises. It's kind of wigging me out a little bit. All right, now, something you need to understand though is when we're rendering, because we haven't clicked, we're just going to render down here. We need to actually pass this model here to the form. We've got the variable, but it's not being passed, so we have to actually pass it. And the names don't have to match, but just for simplistic sake, I mean you could call this fuzzy bunny kitty or whatever, but just for the sake of consistency, they should match. So they're both models, but we have one named model and one named rec short for required. Now when we go in here, you notice how all the fields are reading off of model. When we want to change, you guessed it, name and description to use the required form or the other model. So we're now mixing models here. Let's actually test this out. We're going to create unidentified variable rec. What did we screw up here? Unidentified variable rec. Let's see here. Div class create this render. That's part of the problem here. Let's figure out what's going on here. So we are in petscreate.php at 19. Line 19. Oh, I bet I know what's going on. Okay. If I can find it. Nope. There we go. Petscreate. Line 19. Here's the actual problem here. We are not passing the variable. And this is something, my bad, let me back up and explain this. What's going on here? We are rendering the create view. The controller is saying, here, pass this to create. But the create view is also rendering this underscore form. So we just very simply need to, you guessed it, maybe if I could type here. It's been one of those days, man. I just kind of wanted to like flip my desk over at work and walk out and light the building on fire, but there we go. So now we are just, you know, it's called a cascading variable. We are cascading that variable from one view to the next. So when we go back to here, it should load. Just fine. There we go. Now if we click create, boom, notice how name and description are now suddenly mandatory. So if we just, I don't even know if that's a valid number, I should probably double check categories. So it goes, yeah, one is cats. Cats are always good. All right. So we are going to say test. If we try doing it, you notice how test now is green, but description says cannot be blank. Delete that. Boom. So that is how you would mix and match models. And sure enough, if we actually, whoops, we'll say testing, it'll go ahead and create it. And you see how it says test testing. So that is how you would mix and match models. Now, little side note, if you click on update, it will crash. Boom, because we're using that same underscore form. So we have to go back into the update and make sure that the update now uses the same thing. Now, if you didn't want to do that, you would actually just create a different underscore form. We'd make like underscore update form or, you know, underscore create form or whatever you want to do. But because update is also using the same, we have to do that in both the create controller and the update controller. So good Lord Kitty. What are you doing? He just sounded like a walrus. It was like the weirdest noise anyways. So I hope that was pretty self-explanatory minus my blunder. Really all you have to do if you want to mix and match models is you create both the models, load both the models, and then you have your primary model as it is. You would actually load in the parameters and then be sure to save it. And you know, cascade that variable down to the views. Now, I should back up. If you wanted to, you could have, you know, like we said, you could have inherited this and then changed it. I don't like doing that because, multiple reasons. But here's the main ones. A, I like code reuse. I can now take this model that we've just created, required form. And let's say like we said with categories, we can now have like a name of description in the category and mix and match this in. Or if we make like pet foods, and let's say pet foods has a name of description, we can now use that as well. Also if you go into the database and you make changes, whoopsie, you make changes. Like let's say Bob actually does his job and does this and hits apply. You now have to go into G, and you have to, where is it? You now have to go into G and generate that model, but now you have to also generate the other model, depending on what changes Bob made. And it can get kind of tricky. It can become a real pain in the butt to kind of finagle and figure out. So that's all for this tutorial. I hope you found this educational and entertaining and thank you for watching.