Skip to content

Expressive: Active Record

Expressive Active Record is loosely based on Laravel's Eloquent.

Install

composer require qubus/expressive

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
require('vendor/autoload.php');

use Qubus\Expressive\ActiveRecord\Model;

class Post extends Model
{
    protected string $primaryKey = 'id';

    protected ?string $tablePrefix = '';

    protected ?string $tableName = 'post';
}

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
$posts = Post::all();
foreach($posts as $post) {
    echo $post->title;
}

Find A Model By Primary Key

1
2
$post = Post::find(1);
echo $post->title;

You can also pass multiple IDs or an array to get multiple records.

1
2
3
$posts = Post::find(1, 2, 3);
// or
$posts = Post::find( [1, 2, 3] );

Custom Query

You can still use Active Record to generate a custom query.

1
2
3
4
$posts = Post::where('status', 'published')->get();
foreach($posts as $post) {
    echo $post->title;
}

Or if you only want to retrieve the first record, you can use first() method.

1
2
$post = post::where('status', 'published')->first();
echo $post->title;

Create

Creating a New Model

1
2
3
4
5
6
$post =  new Post();

$post->title = 'A New Title';
$post->content = 'More content to come...';

$post->save();

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
echo "New Post ID: " . $post->id;

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
$post = Post::create( ['title' => 'A New Title', 'content' => 'More content to come...'] );

// create() method will return a newly created model or false if inserting fails
echo $post->id;

Updating Models

Updating Retrieved Models

1
2
3
4
$post = Post::find(1);

$post-title = 'A Different Title';
$post->save();

Mass Updating

You still can use Active Record to generate a custom query before updating.

1
Post::where('status', 'published')->update( ['title' => 'A Different Title'] );

Or alternatively you can call update() method right away.

1
Post::update( ['title' => 'A Different Title'], ['id' => 1] );

Deleting Models

There are several ways to delete model

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Delete retrieved model
$post = Post::find(1);
$post->delete();

// Delete a single model by its primary key
Post::delete(1);

// Delete multiple models by their primary keys
Post::delete(1, 2, 3);
//or
Post::delete( [1, 2, 3] );

// Use Active Record
Post::where('status', 'published')->delete();