Model class provides an elegant ORM (Object-Relational Mapping) for interacting with your database. Each database table has a corresponding Model which is used to interact with that table.
Defining models
Create a model by extending the baseModel class:
Properties
The database table associated with the model
The primary key column name
Columns that are mass assignable
Columns that are protected from mass assignment
Retrieving models
find()
Find a single record or multiple records by filter criteria.ID or array of filter conditions
Specific columns to retrieve (defaults to all)
Returns Model instance, array of Models, or null if not found
Creating models
create()
Create a new record in the database.Associative array of column names and values
Returns the newly created Model instance
createMany()
Insert multiple records at once.Array of associative arrays representing records
Returns array of created Model instances
Updating models
update()
Update one or more records.Array of column values to update
Filter conditions for which records to update
Returns Model instance for method chaining
save()
Save changes to an existing model instance.Returns result of the save operation
Deleting models
delete()
Delete one or more records.Filter conditions for records to delete
Returns Model instance for method chaining
Committing changes
commit()
Execute pending insert, update, or delete operations.Returns result of the database operation
Relationships
hasOne()
Define a one-to-one relationship.Related model class name
Foreign key on related table (auto-detected if null)
Local key on parent table (auto-detected if null)
Returns HasOne relationship instance
hasMany()
Define a one-to-many relationship.Related model class name
Foreign key on related table (auto-detected if null)
Local key on parent table (auto-detected if null)
Returns HasMany relationship instance
belongsTo()
Define an inverse relationship.Related model class name
Foreign key on parent table (auto-detected if null)
Owner key on related table (auto-detected if null)
Returns BelongsTo relationship instance
belongsToMany()
Define a many-to-many relationship using a pivot table.Related model class name
Pivot table name (auto-detected if null)
Foreign pivot key (auto-detected if null)
Related pivot key (auto-detected if null)
Local key on parent table (auto-detected if null)
Related key on related table (auto-detected if null)
Returns BelongsToMany relationship instance
hasManyThrough()
Define a has-many-through relationship.Final related model class name
Intermediate model class name
First foreign key (auto-detected if null)
Second foreign key (auto-detected if null)
Local key on parent table (auto-detected if null)
Local key on intermediate table (auto-detected if null)
Returns HasManyThrough relationship instance
Utility methods
getPrimaryKey()
Get the primary key name for the model.Returns the primary key column name
getTableNameFromModel()
Get the database table name for the model.Returns the table name