PHP code example of toutaphp / ogam-data-mapper

1. Go to this page and download the library: Download toutaphp/ogam-data-mapper 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/ */

    

toutaphp / ogam-data-mapper example snippets



namespace App\Entities;

class User
{
    public function __construct(
        public int $id,
        public string $name,
        public string $email,
        public DateTimeImmutable|null $email_verified_at,
        public string $password,
        public string|null $remember_token,
        public datetime $created_at,
        public datetime $updated_at,
    )
    {}
}



namespace App\Mappers;

use Touta\Ogam\Attribute\Mapper;
use App\Entities\User;

#[Mapper(resource: 'database/mappers/UserMapper.xml')]
interface UserMapper
{
    public function findById(int $id): ?User;
    public function findByStatus(?Status $status): array;
    public function insert(User $user): int;
}



use Touta\Ogam\SessionFactoryBuilder;

// Build session factory
$factory = (new SessionFactoryBuilder())
    ->withConfiguration(base_path('config/ogam.xml'))
    ->build();

// Open session
$session = $factory->openSession();

// Get mapper
$userMapper = $session->getMapper(UserMapper::class);

// Query
$user = $userMapper->findById(1);

// Insert
$newUser = new User(0, 'john', '[email protected]', Status::ACTIVE, new DateTimeImmutable());
$userMapper->insert($newUser);
echo $newUser->id; // Generated ID

// Commit and close
$session->commit();
$session->close();