PHP code example of kenny1911 / doctrine-dbal-hydrator

1. Go to this page and download the library: Download kenny1911/doctrine-dbal-hydrator 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/ */

    

kenny1911 / doctrine-dbal-hydrator example snippets


use Doctrine\DBAL\Types\Types;
use Kenny1911\DoctrineDbalHydrator\Mapping\Attribute\Column;

final readonly class User
{
    public function __construct(
        #[Column]
        public string $id,
        #[Column(name: 'login')]  // Maps SQL column `login` to property `username`
        public string $username,
        #[Column]
        public Role $role,       // Enum support
        #[Column(type: Types::DATETIME_IMMUTABLE)]
        public \DateTimeImmutable $registeredAt,
    ) {}
}

use Kenny1911\DoctrineDbalHydrator\Hydrator;
use Kenny1911\DoctrineDbalHydrator\ObjectHydrator;
use Kenny1911\DoctrineDbalHydrator\Mapping\AttributeLoader;

/** @var \Doctrine\DBAL\Connection $conn */
$hydrator = new Hydrator(
    objectHydrator: ObjectHydrator::createByConnection($conn),
    loader: new AttributeLoader(),
);

$data = $conn->fetchAssociative(
    query: 'SELECT * FROM users WHERE id = :id',
    params: [
        'id' => 'f4117ad3-5914-493d-90e1-4371832e82f4'
    ],
);

$user = $hydrator->hydrate(User::class, $data);