Skip to content

Expressive: Datamapper

Simple Datamapper ORM part of the expressive package.

Install

composer require qubus/expressive

Usage

Create Entity Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require('vendor/autoload.php');

use Qubus\Expressive\DataMapper\Entity;
use Qubus\Expressive\DataMapper\Property;
use Qubus\Expressive\DataMapper\SerializableEntity;

#[Entity('post')]
class Post extends SerializableEntity
{
    #[Property('id')]
    public int|string $id;

    #[Property('author')]
    public int $author;

    #[Property('category')]
    public string $category;

    #[Property('title')]
    public string $title;

    #[Property('slug')]
    public string $slug;

    #[Property('content')]
    public string $content;

}

Call Datamapper

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use Qubus\Expressive\DataMapper\PdoDataMapper;

// Instantiate the data mapper by passing a PDO instance and the entity class name.
$dm = new PdoDataMapper($pdo, Post::class);

// Retrieve all posts
$dm->findAll();

// Retrieve all posts by category
$dm->findAllBy('category', 'General');

// Retrieve a single post with the id = 1
$dm->findOne(1);

// Save a post instance to the database
$post = new Post();
$post->category = 'Living';
$post->title = 'How to Create A Lavish Lifestyle';
$post->content = 'Living a lavish lifestyle is not ...';
$post = $dm->create($post);

// Update a post
$post->category = 'Lifestyle';
$post = $dm->update($post);

// Delete a post with the id = 1
$dm->delete(1);