Create models in CodeIgniter 4

- Andrés Cruz

En español
Create models in CodeIgniter 4

Now we are going to see how we can create a fundamental component in our MVC, which has been the component that allows us to work with the data layer, which cannot be other than the model, the model is simply a class, just as it happens With other frameworks like Laravel that extends Model, for the model, unlike version 3, we have to define several attributes to be able to use it or to be functional.

Remember that the primary function of a model is to connect a model to a table in the database.

We have to indicate the insertable fields, that is, those fields that we are going to use to manage said table linked to the table; in other words the columns; for example, let's start with a table like the following:

Tabla en MySQL

Therefore, we want to manage all the columns of the previous database table for our model, and following the comments above:

We also have to indicate the property called table, to indicate which table our model matches with which table in the configured database:

class MovieModel extends Model {    protected $table = 'movies';    protected $primaryKey = 'id';    protected $allowedFields = ['title', 'description','category_id']; }

You are about to name the most important properties that you can use to define the model, you can see the rest of the properties that you can define for the model in the official documentation; some very interesting properties would be for example.

$returnType

Here we can indicate if we want by default the data that we can obtain by making select type queries to the database, return them in an array type structure ('array') or object type ('object').

$createdField

Here we can indicate the name of the field for the creation date, and it will be established when we create a new record.

$validationRules

With this property we can indicate the validation rules on the columns of our model that will be used in the creation and update phase.

Otros datos sobre los modelos

  • In the models our business logic goes, components such as specific functions that we want to use within the app, such as get all or a set of records are simply functions that we create for each process; for example, a function to get all records.
  • When we work with queries, there are functions that we can use for free (already provided by the framework) to work with the records; for example, the method called find, to obtain a record, or the first, insert, save, update whose names are self-explanatory or indicate what function they perform.
  • Models are a component that matches migrations, since it is usual that there is a one-to-one correspondence between tables, migrations and models, in the case of CodeIgniter 4, migrations are a separate file and composition that the of the model, that is, they are independent, unlike other frameworks such as Django or Flask, whose migration composition is generated from the models.
Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.