PHP code example of tithely / picomapper
1. Go to this page and download the library: Download tithely/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/ */
tithely / 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')
->withDeletionTimestamp('deleted');
$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);
$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');
$post = [
'id' => 'abc123',
'author' => [
'id' => 'zxy321',
'name' => 'John Doe'
],
'title' => 'Data Mappers Rock',
'content' => 'They save you time',
'comments' => [
[
'id' => 'def456',
'post_id' => 'abc123',
'author' => [
'id' => 'zxy321',
'name' => 'John Doe'
],
'content' => 'Did you like my post?'
],
[
'id' => 'hij789',
'post_id' => 'abc123',
'author' => [
'id' => 'klm012',
'name' => 'Jane Doe'
],
'content' => 'Nice article!'
],
]
];
$mapper->save($post);
$saved = $mapper->eq('id', 'abc123')->findOne();
// $saved will be identical in structure to post
$mapper->registerHook('updated', function ($table, $key, $updated, $original) {
printf('Table %s (ID: %s) was updated...', $table, implode(':', $key));
});