Main properties of models in CodeIgniter 4

- Andrés Cruz

En español

Main properties of models in CodeIgniter 4

The model layer in the MVC (Model-View-Controller) design pattern is the layer that handles business logic and communication with the database. That is, it is the layer that performs the CRUD operations (Create, Read, Update and Delete) in a database, usually a model governs a table in the database. Let's see the most common properties that we can apply to our models in CodeIgniter 4 and what they are for.

$table

Specifies the database table with which this model works.

$primaryKey

This is the name of the column that uniquely identifies the records in this table. This does not necessarily have to match the primary key that is specified in the database; used with methods like find() to find out which column to match the specified value against.

$useAutoIncrement

Specifies whether the table uses an auto-increment function for $primaryKey. If set to false, you are responsible for providing the primary key value for each record in the table.

$returnType

This setting allows you to define the type of data that is returned. Valid values are 'array' (the default value), 'object', or the fully qualified name of a class that can be used with the getCustomResultObject() method of the Result object.

$useSoftDeletes

If true, any calls to the delete() method will set the delete_at to the database, instead of deleting the row. This can preserve data when it can be referenced elsewhere, or it can maintain a "recycle bin" of objects that can be restored, or even just preserve it as part of a security trace. If true, the find*() methods will only return undeleted rows, unless the withDeleted() method is called before the find*() method is called.

$allowedFields

This array should be updated with the field names that can be set during the save(), insert() or update() methods. Any field name other than this will be discarded. This helps protect the integrity of columns that we don't want the application to be able to interact with.

$useTimestamps

This boolean value determines whether the current date is automatically added to all inserts and updates. If true, it will set the current time in the format specified by $dateFormat. This requires that the table have columns named created_at and updated_at in the appropriate data type.

$createdField

Specifies which database field to use for the data record creation timestamp.

$updatedField

Specifies which field in the database should be used to hold the data record update timestamp.

These are some of the most common configurations and you can see the full list here:

https://codeigniter.com/user_guide/models/model.html#id6

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 4d 08:06!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!