 Hey everybody, this is Brian. Welcome to the 10th E2 tutorial. Today we'll be talking about transactions. So transactions are a bit of a complex topic, and typically people don't cover these until you've already spent, you know, a couple dozen hours doing tutorials and learning. I actually want to cover them earlier because it'll teach you good coding patterns and, well, it'll save you a lot of headache if you're trying to work on a project at home. So a transaction is a way of taking multiple models and making sure that they all validate. Now what does that mean? Let's look at our database here. We have our accounts, and let's say we want to add multiple accounts. So we're going to write some code to do that. But if you notice how, let's actually go into our altar table here. Email and password are set to nn or not null, meaning they're required fields. So let's go into ye. Look at our accounts. You see very in the model it's required. So if we don't add an email or password, it will silently fail, meaning there'll be no error message for the end user. The end user thinks that everything just worked out fine. So we're going to add some classes here. So we're going to use our accounts model, and we're going to use the exemption class. All right, we'll go down to our test here. And there's nothing in here, so we're going to start fleshing this bad boy out. So we're going to say, whoopsie. Now if you're used to the old version of ye, notice the dollar sign in app used to be just a pp, but now it's dollar sign app. And the reason for that is I believe they moved it from a class to a variable. Don't quote me on that though. So we're going to say app.database. So what is this? We haven't seen this before. We're saying the ye namespace, grab the app variable, and then load a component called the db or database. If you have no idea what that is or where it's getting it, it's very simple. The ye namespace, there's a variable called app, and there's a component called db. Well, how do we know what components we have? If you open up web, you'll see this is our config file, and you'll see components. And these are all the components that get loaded when ye loads. One of them is db, which requires the current directory dbphp, which we've already filled out for our database connection. So this could be a connection to a oracle, database, sql, mysql, etc, etc. It just uses the connection class. That's really all that is. Alright, so we're grabbing. We actually don't even want to do that. We want to go begin transaction. So we now have a transaction here. We need to do something with that transaction. Now think of a transaction as you're standing in line at the bank, right? You finally get up to tell her it's your turn. You get to do things. You want to move money from here to here. You want to cash your paycheck. You want to do this. You want all those things to finish before you walk away. That's what a transaction is. So we're going to do this in what's called a try catch block. If you fell asleep during your PHP classes, this is basic error handling 101. So what we're going to do is do something. And then we're going to say transaction commit. Meaning we're going to say, yes, we want that to actually, you know, happen. And if there's a problem, for example, if the bank teller looked at you and said, you don't have enough money to do that, you want to say transaction rollback. Meaning you want to go right back to the way things were before you even started. It's kind of like when you're a little kid, you get a do over. So if you try this and it doesn't work and say, you know what, I want to roll back to the way it was back up here. Pretty neat, huh? So let's see this in action. We're going to go a cat. We're going to make a for loop. But if you're wondering how I get intelligence to pop up, it's just, I think it's control, control alt, control space. Yeah, control space. There we go. It was doing it and I forgot which one it was. So we're going to say from zero to four. So we're going to make five of these bad boys. Get that out here. Let's say model equal new accounts. And then we're going to do something with that. And then we're going to say model save. Now, there's going to be a basic flow to this that you're not going to fully understand until we're done. It's a little confusing. And that's why they kind of leave this for advanced. But I think we're smart enough that you're going to get this. So we're going to say model email. And let's do email. Whoops. I like actually putting spaces between those. And we're going to say model password because they're both required. Ultra secure password. This is not actually how you do a password. We'll cover that in a future tutorial. But we're just throwing a generic password in there. So when we go to model save, this will of course save because email and password are required. See, they're in our model. Remember the model is a representation of the data. Now what happens if we blank out the password? Let's actually do this. If we go this route, and let's try this real quick, right? Let's load up our page. Let's go here. Let's look at our log and see what happens. You'll see how model not inserted due to validation error. We get a couple of these things, meaning the model knows that bad things happen, but you see there's a commit transaction. So nothing actually happened because we had validation errors. We go on to our database here and sure enough after a refresh it's still empty. So that's model validation. Now we have to get it working with our transaction. Notice how, and let's actually go back and let's refresh a couple of times just so you can see that nothing's happening. There's no error message, no nothing. We have no way of knowing without looking at the log that something went boom. So what we want to do is we want to go back in here. And we want to say, if not model, validate, throw, new, exemption. And the exemption class, if you've never worked with it, is actually just very simple. It's just basically a wrapper around, well, around errors. It's really all it is. So we're going to say, oh, model, I had it myself there, sorry, index could not be validated. Make sure we got that. And we are just going to through the magic of copy and paste. And I like to do a validate and a save on different lines. The reason being, you could put these both together, but let's say the model validated, but there was a problem with the database. So then you won't know which one happened. So this way they're separated. So you know the model validated, but we couldn't say because the database was offline or something screwy happened, right? Now we're going to roll back here and we're going to actually say yee, warning. And this is where we would update, you know, the end user. Let's actually do this. Whoops. I'm going to say get message because we're going to get that. And it's worth noting there is actually a lot of stuff in the exemption class. If you look at this, we can get the code, the file, the line, the name. We can get a full, you know, stack trace as a string. But we're just going to get the message, which of course is what we just entered up here. And then we're going to roll back. Otherwise we're going to say commit and then yee, info, all models saved. Remember a transaction is an all or nothing kind of thing. So let's try this and see what happens here. Let's refresh this. Notice how now we've got this little warning down here. So let's click that. And we see model zero could not be validated. Rollback transaction. That's because we've got that try catch there. I didn't even try to execute the rest of it because it already found that, hey, something went boom. So let's do what I call the Bob factor here. And what we're going to say is, whoops, get rid of those. If index greater or less than, and we'll say two. Actually, I'll say not equals. Let's do it this way. I call it the Bob factor because I used to work with this programmer named Bob, who was just a royal pain to work with. And he would just do stupid things in code and leave you scratching your head. So what we're going to say is every single one of these, but the number two, which is actually the third one because we're zero based, is going to have a password. And let's see what happens here. All right, let's give it a good clicky. And you can see how we've got insert into insert to model to cannot be validated rollback transaction. Now you see these insert statements. So we're talking to the database. Let's go out to the database here and see what it looks like. Let's refresh it. There's nothing there because that's what a transaction is. It's all or nothing. Now just to prove that this code works, let's actually get rid of the Bob factor here. So it doesn't matter which one it's going to give it a password and then it will validate. Go back here and notice how there's no error message now. And we can see we've got a bunch of inserts and then a commit transaction and then all models saved. And if we go out to our database, we should see zero one, two, three, four. Actually no four, but you know what I mean. So that is transactions in a nutshell. Why would you use a transaction? Well in a real world, you're probably not going to bulk create a bunch of accounts like this. What you're going to do is you're going to create an account. But then that account needs special settings in a different table. Like maybe they have a special pet that they get automatically for free. And you want all that to be done before it's persisted down to the database. And if there's a problem anywhere along that chain, you want to stop and say, hey, there was a problem. So pretty simple, pretty easy to understand, but it's actually an advanced topic. And there is more to it, but we're not going to cover it in this tutorial because I just wanted to throw this out because I've already gotten a few dozen emails saying, how do I work with transactions? So that in a nutshell is how you would work with a transaction. Well, I hope you found this educational entertaining and be sure to visit my website voidroms.com. I actually haven't put the source for these tutorials out here yet because it's been very simple. But there is plenty of source code for all the other tutorials and languages. And I'll try to get this out here soon. Also, this is 100% funded by your donations. Feel free to donate, but if you're a starving college student, do not. And if you are still watching the video, you're in luck because we have a Voidroms Facebook group. And there is like 400 and some odd programmers out there. And we are all fluent in many languages and we all just help each other out. So go ahead and jump out to Facebook and join the Voidroms Facebook group and just ask any questions you got.