 Hey everybody, it's Brian. Welcome to the 12th Yee tutorial. Today we're going to be creating with models. And by creating, I mean manually. We've kind of gone in here and you can create and key in some data, but what if you want to do this programmatically? Let's just clean up some of this clutter from the last tutorial here. Alright, so, whoops, if you've been following along, we've got this test.php. Now, this is where I have to pause and I have to remind you, kind of a pop quiz here, we're going to programmatically do something. Where do we want to do that? This is a model view controller framework. Model is a representation of the data. Controller controls the models in the view and the view simply loads data and displays it. Because this is, you know, just a simple PHP framework, well I shouldn't say simple, but you know what I mean. We could actually do it here in the view, but we really don't want to. What we want to do is in the controller. I can go up to controller. And here's why. Very simply, you just want to be able to control the flow of data and that's what the controller does. If you start modifying data in the view and then later somebody else or yourself has come back and troubleshoot it, you're going to have to figure out where that data is actually being modified. So keep things uniform and modify your data in the controller. So we're in the action test and we're going to actually say model equal new teachers. So what we're doing here is we're just creating a new model. Now we need to pass that model to the view. What is going on here? Why isn't that working? Oh, my bad. Yes, sorry, I forgot. Yes, we have to pass an array of parameters to it. There we go. So what we're doing here is we're creating a model and we're now passing that as a variable called model to the view test. And we should actually just for the sake of argument here, model and we'll call it teacher. Teacher, sorry, plural. Oops. Let's get some PHP action going on here. Then we can say if is set and model. You notice how it automatically knows that the model is a variable and it is a teacher's class. That's because of this var up here. And let's just say if not is set, then we're just going to return. Otherwise we'll start doing some stuff. And we're just going to say var, let's echo it. Echo, fair dump, model. If the model exists, then we're going to just echo a variable dump out. Alright, so let's go back to... So what we've done here is we've created a model class and we're now passing it to the view. Notice how we haven't actually done anything with the model class. We've just simply created the variable. But you can see from this object dump what's going on here. And you can see that it's a new C active record. It's private and it's got some attributes to it. We haven't actually modified the database yet. We're just simply passing it to the view. So let's actually do that. I'm going to say first name, equal. I need a good first name. Slarty. And I'll say model last name, April Bartfast. Anybody who's ever read Snickers Guide to the Galaxy is probably snickering right now. So we can say model Slarty Bartfast. Now I want to demonstrate something just for the sake of demonstration here. Let's go back here and let's actually do an F and let's look for the word Bart. So we've got Slarty Bartfast and there's Slarty and Bartfast. So we passed the model with the variable in it. Let's go out to the database real quick here. There's no Slarty Bartfast in the database. So adding the data to the model does not save it to the database. To actually save it, well you guessed it. You need to save it. Pretty simple, huh? I love it when things are easy. Once you save it, you will actually create the SQL string that is needed to insert the data into the database for you. Doing so it will automatically trap any custom characters like the quotes and single quotes or slashes and things like that. I mean it does all that for you. It's actually pretty slick the way it does it. So we're creating a model, adding first name, last name, and saving. So we're going to go back to our view here. Notice how there's a lot more information. Well that's because now that it's actually persisted into the database, we have to hold information about where it is in the database such as the primary key. Notice how we didn't have to go out and do a lookup for ID teachers but it automatically knows that it's 10. So if we go out to the database and do a lookup on 10, it'll be Slarty Bartfast. I never thought I would ever say in my life it'll be Slarty Bartfast. Thank you, Hitchhiker's Guide to the Galaxy. Anyways, so let's just refresh our database and sure enough on ID 10 is Slarty Bartfast. Now this is another pitfall I want to talk about. You look at your code what happens the next time somebody goes to the test action? Well it's going to create a brand new teacher with the same name. To demonstrate we're just going to refresh the page. Excuse me. And notice how the primary key is now 11 but it's the exact same information. And that's because well we haven't marked that as unique. By that I mean name or last name. And just jump into the database here and if we do a refresh we should have 10 and 11 that are exactly the same. That's all for this tutorial. Next one we're going to demonstrate how to find specific records and if we're feeling a little adventurous we may actually delete one.