From the course: PHP: Object-Oriented Programming with Databases

Active record design patterns

- [Instructor] In this chapter, we're going to learn how to define a database driven class. There are a number of different techniques that we could use to do this, but I think that the best one is to use something called the Active Record Design Pattern. The Active Record Design Pattern is a very popular design pattern for using databases with OOP. PHP frameworks like Laravel, CakePHP, Yii, and CodeIgniter all use this Active Record Design Pattern. The idea is that the in-memory objects, that is the instances of a class that we create are going to correspond to a single row in our database table. The object's properties are going to match the table columns, and the object is also going to have methods to allow it to easily perform the basic database operations of create, read, update, and delete. Let me illustrate. Let's say that we have a table called bicycles that's in our database and it has columns for brand, model, year, et cetera. What we want to be able to do is to retrieve a row from that database so that we have the values that are stored there, and then take those values and automatically populate them into a instance of a class. So, if we have the bicycle class and it has properties for brand, model, year, et cetera, we would take the values from the database row and use them to create a new instance that has properties with the same values. Because this happens automatically, it feels almost like we're working directly with the database row. The same process also works in reverse. Let's imagine that we create a new instance of the bicycle class, and we assign values for its brand, its model, its year, et cetera. Then, we can take those and we can tell it to save them back to the database. So, then our in-memory object looks exactly the same as the row in the database. All of those properties in the object get translated into values in a database row. The same thing works with update. You retrieve values out of the database, you make changes to them after they've been created as an object instance, and then you save them back to the database again. Delete works the same way. There's essentially two processes. The first is that we take database records, retrieve them, and their values are used to populate the properties of objects. Second, an object's properties are used to craft SQL statements that we can submit to the database. In PHP code it looks something like this. Let's say that we create a new instance of a customer object, and then we assign values to several of its properties. At this point, this is just simply a PHP object that has some values assigned to its properties. But the active record portion is that when we call save on that object, it's able to take those properties and convert them into an insert statement that can be submitted to the database all by just calling one single method. The same thing works in reverse. Let's say that we want to find a customer from the database. We can ask it to retrieve a customer. It would create a new object and automatically populate all of the properties for that object with the values that were stored in the database. We can make changes to them by simply setting new values to the property and calling save again. It's a powerful, intuitive way for us to work with the rows of data that are in a database. We're first going to implement the Active Record Design Pattern by learning to find records and to create instances on them. That first process that we talked about. Then in the next chapter, we'll work on creating, updating, and deleting records.

Contents