Let's use our existing model to connect and load database data in CodeIgniter.
- [Instructor] Unlike other frameworks, once we load a model, CodeIgniter does not automatically connect to a database. We must explicitly tell the model that we wish to connect to a database. With that, CodeIgniter gives the developer the freedom to use the model as he or she wishes. While it is the most common thing to do, not all models connect to a database. We could have a model that connects to a web service, another model that opens a file structure, and so on. A model is just a data and business layer, and such data may or may not be a database.
For now, we'll stick to databases and see how to connect to a database using a model. Please refer to the setting up the local environment movie if you haven't done so already. Let us open up our files. Desktop, exercise files, chapter two, 02 01 start. Let's start up our server with desktop, exercise files, chapter two, 02 01 start, and let's start the server with our environment variables. So we can connect to a database correctly.
And we go to the browser, refresh, and no error means that we are connecting to the database correctly. So let's open up our model in application models. Property.php And we're going to move this part of the code into the constructor so every time the class is called or loaded, we'll also want this available to the whole class. So we'll do this by adding a variable. This, db, equals database.
So now let's add a method to get the database version. Public, function, get version. And what we'll do here is set up a result set and generate a database query. With this, db query. Inside the query, we'll be talking to the database directly. So let's type select version, and let's return to result set. Return, result set. Since we've auto-loaded the model, it's available throughout the whole application.
Now let's fetch the data from the database and send it to the view to be rendered on the browser. So let's open up our controller, and let's go to the show method and add a new variable called version. We do this by using this property, get version. And now let's add that to the data array that we're gonna pass to the view. Data version is equal to version conn_id and server info.
And now let's open up the view. Properties, show, and let's add our version to our HTML. So with our HTML paragraph, we'll just say MySQL version and let's open up and close php tags and let's just echo version. Let's go back to the browser and refresh, and we have the version. That is how we have confirmed that our application is connecting to the database correctly. The suggested way to go is to have a model for every table on the database structure.
In the demo application, we should have a property model for the property stable, a status model for the status stable, and an agent model for the agent stable.
- Loading views and data from containers
- Routing with controllers
- Creating models
- Configuring the data
- Working with data, forms, sessions, and headers
- Logging and error logging
- Extending CodeIgniter to consume dependencies