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;
}
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);