πŸ“Œ Full List of protected Variables in CodeIgniter 4 Model

In CodeIgniter 4, Model properties (protected variables) control how the model interacts with the database. Here’s a complete list:

πŸ“Œ 1. Table & Primary Key

protected $table = 'table_name'; // Table name in the database
protected $primaryKey = 'id'; // Primary key column name
protected $useAutoIncrement = true; // Set false if using manual IDs

$table → Specifies the table name.

$primaryKey → Defines the primary key column.

$useAutoIncrement → If true, CodeIgniter will auto-increment primary keys.

πŸ“Œ 2. Allowed Fields for Insert/Update

protected $allowedFields = ['column1', 'column2', 'column3'];

Specifies which columns can be inserted/updated via insert() and update().

Prevents Mass Assignment Attacks.

πŸ“Œ 3. Timestamp Management

protected $useTimestamps = true; // Enables automatic timestamps
protected $createdField = 'created_at'; // Column for created timestamp
protected $updatedField = 'updated_at'; // Column for updated timestamp

If useTimestamps = true, CodeIgniter automatically fills timestamps on insert/update.

If your table uses custom timestamp names, update createdField and updatedField.

πŸ“Œ 4. Soft Deletes (Optional)

protected $useSoftDeletes = true; // Enables soft delete
protected $deletedField = 'deleted_at'; // Column for soft deletes

Soft delete means records are not permanently deleted, only marked as deleted.

Instead of DELETE FROM table, it sets deleted_at = current_timestamp.

πŸ“Œ 5. Validation Rules (Optional)

protected $validationRules = [
    'name' => 'required|min_length[3]|max_length[255]',
    'email' => 'required|valid_email'
];

protected $validationMessages = [
    'name' => [
        'required' => 'Name is required',
        'min_length' => 'Name must have at least 3 characters'
    ]
];

protected $skipValidation = false; // Set true to bypass validation

$validationRules → Defines validation rules for form input.

$validationMessages → Custom error messages for each field.

$skipValidation = true → Disables validation.

πŸ“Œ 6. Return Type (Optional)

protected $returnType = 'array'; // Return results as an array (default)

Possible values: 'array', 'object', 'App\Entities\YourEntity'.

πŸ“Œ 7. Database Connection (Optional)

protected $DBGroup = 'default'; // Connects to default database

If using multiple databases, change default to another group name in app/Config/Database.php.

πŸ“Œ 8. Enable Automatic Data Protection (Optional)

protected $allowCallbacks = true; // Allows model callbacks

If true, enables before/after insert/update/delete callbacks.

🎯 Full Example Model

namespace App\Models;

use CodeIgniter\Model;

class ManagerModel extends Model {
    protected $table = 'managers';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    
    protected $allowedFields = ['user_id', 'hotel_id', 'name', 'email'];
    
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    
    protected $useSoftDeletes = true;
    protected $deletedField = 'deleted_at';

    protected $validationRules = [
        'name' => 'required|min_length[3]',
        'email' => 'required|valid_email'
    ];
}

πŸš€ Summary of protected Variables

Variable

Purpose

$table

Defines the database table

$primaryKey

Defines the primary key column

$useAutoIncrement

Enables/disables auto-increment for primary key

$allowedFields

List of columns that can be inserted/updated

$useTimestamps

Enables automatic timestamps (created_at, updated_at)

$createdField

Defines the column for creation timestamp

$updatedField

Defines the column for update timestamp

$useSoftDeletes

Enables soft delete instead of permanent delete

$deletedField

Defines the column for soft delete timestamp

$validationRules

Sets validation rules for fields

$validationMessages

Custom error messages for validation

$skipValidation

Disables validation if true

$returnType

Defines return type (array, object, entity)

$DBGroup

Specifies which database connection to use

$allowCallbacks

Enables model callbacks (beforeInsert, afterUpdate, etc.)

Share:

0 Comments:

Post a Comment