Expressive: Active Record
Expressive Active Record is loosely based on Laravel's Eloquent.
Install
Usage
Defining Models
Models in Active Record represent a single table to work with. To define a model, you need to extend the Model class.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The $tablName
property is used to tell which table the model will work with. There are also several other properties to customize the model configuration.
Model Properties
$tableName
: to define the table name. This property is required.$tablePrefix
: to define a prefix for table names. This property is optional.$primaryKey
: to define the column name of the table's primary key. Default is "id". If your PK has other name than "id" you should change this.$incrementing
: to define whether your PK is auto-increment. Default value is true. If you'd like to generate your Primary Key value by custom function, set this to false.
Querying
Retrieve All Models
1 2 3 4 |
|
Find A Model By Primary Key
1 2 |
|
You can also pass multiple IDs or an array to get multiple records.
1 2 3 |
|
Custom Query
You can still use Active Record to generate a custom query.
1 2 3 4 |
|
Or if you only want to retrieve the first record, you can use first()
method.
1 2 |
|
Create
Creating a New Model
1 2 3 4 5 6 |
|
After saving the record, if your model uses an auto incrementing primary key, the generated insert id will be set to the object. So if you use example above, you can show the new post's ID.
1 |
|
Note
Note that the property isn't always id
. It depends on the primaryKey property you've set.
Alternatively, you can use create()
method to create new models.
1 2 3 4 |
|
Updating Models
Updating Retrieved Models
1 2 3 4 |
|
Mass Updating
You still can use Active Record to generate a custom query before updating.
1 |
|
Or alternatively you can call update()
method right away.
1 |
|
Deleting Models
There are several ways to delete model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|