PHP code example of rrigby / picomapper

1. Go to this page and download the library: Download rrigby/picomapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rrigby / picomapper example snippets


use PicoDb\Database;
use PicoMapper\Mapper;
use PicoMapper\Definition;

$db = new Database([...]);
$mapper = new Mapper($db);

$author = (new Definition('authors'))
    ->withColumns('name')
    ->readOnly();

$comment = (new Definition('comments'))
    ->withColumns('content')
    ->withOne($author, 'author', 'id', 'author_id')
    ->withDeletionTimestamp('deleted');

$post = (new Definition('posts'))
    ->withColumns('title', 'content')
    ->withOne($author, 'author', 'id', 'author_id')
    ->withMany($comment, 'comments', 'post_id')
    ->withDeletionTimestamp('deleted');
    
$mapping = $mapper->mapping($post);

// comments.post_id = posts.id
$post->withMany($comment, 'comments', 'post_id');

// authors.id = posts.author_id
$post->withOne($author, 'author', 'id', 'author_id');

// posts_tags (post_id, tag_id) links posts to tags
$tag = (new Definition('tags'))->withColumns('name');

$post->withManyByJoin($tag, 'tags', 'id', 'id', 'posts_tags', 'post_id', 'tag_id');

$comment = (new Definition('comments'))
    ->withColumns('content')
    ->useAutoIncrement();

$post = (new Definition('posts'))
    ->withColumns('title', 'content')
    ->withDeletionTimestamp('deleted')
    ->withDeletionData(['deleted_by' => $userId]);

$post = (new Definition('posts'))
    ->withColumns('title', 'content')
    ->withOne($author, 'author', 'id', 'author_id')
    ->withMany($comment, 'comments', 'post_id')
    ->withCreationData(['date_entered' => gmdate('Y-m-d G:i:s')])
    ->withModificationData(['date_modified' => gmdate('Y-m-d G:i:s')])
    ->withDeletionTimestamp('deleted');

$data = [
    'id' => 'abc123',
    'author_id' => 'zxy321',
    'author' => [
        'id' => 'zxy321',
        'name' => 'John Doe'
    ],
    'title' => 'Data Mappers Rock',
    'content' => 'They save you time',
    'comments' => [
        [
            'id' => 'def456',
            'post_id' => 'abc123',
            'author_id' => 'zxy321',
            'author' => [
                'id' => 'zxy321',
                'name' => 'John Doe'
            ],
            'content' => 'Did you like my post?'
        ],
        [
            'id' => 'hij789',
            'post_id' => 'abc123',
            'author_id' => 'klm012',
            'author' => [
                'id' => 'klm012',
                'name' => 'Jane Doe'
            ],
            'content' => 'Nice article!'
        ],
    ]
];

$mapper->mapping($post)->save($data);
$saved = $mapper->mapping($post)->eq('id', 'abc123')->findOne();

// $saved will be identical in structure to $data

$mapper->registerHook('updated', function ($table, $key, $updated, $original) {
    printf('Table %s (ID: %s) was updated...', $table, implode(':', $key));
});

$mapper->startTransaction();
$mapper->mapping($post)->save($data);
$mapper->mapping($comment)->save($commentData);
$mapper->closeTransaction();

// Queries logged by PicoDb
print_r($mapper->getLogMessages());