PHP code example of kdevhubin / pdoentitygenerator

1. Go to this page and download the library: Download kdevhubin/pdoentitygenerator 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/ */

    

kdevhubin / pdoentitygenerator example snippets




declare(strict_types=1);

namespace App\Entity;

class TestMyTable
{
    private ?int $id = null;
    private string $name = '';
    private string $email = '';
    private ?\DateTimeImmutable $createdAt = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(?\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }
}



declare(strict_types=1);

namespace App\Repository;

use App\Entity\TestMyTable;

class TestMyTableRepository
{
    public function __construct(
        private readonly \PDO $pdo,
    ) {
    }

    public function find(int $id): ?TestMyTable { ... }
    public function findAll(): array { ... }
    public function insert(TestMyTable $entity): TestMyTable { ... }
    public function update(TestMyTable $entity): TestMyTable { ... }
    public function delete(int $id): bool { ... }
}

// Option 1: Use the static create() method (recommended)
$repository = \App\Repository\TestMyTableRepository::create();

// Option 2: Manual PDO injection
$pdo = \App\Factory\PdoFactory::create();
$repository = new \App\Repository\TestMyTableRepository($pdo);

// Option 1: Use the static create() method (uses PdoFactory internally)
$repository = \App\Repository\TestMyTableRepository::create();

// Option 2: Manual PDO injection via PdoFactory
$pdo = \App\Factory\PdoFactory::create();
$repository = new \App\Repository\TestMyTableRepository($pdo);

// Option 3: Manual PDO connection
// $pdo = new \PDO('mysql:host=127.0.0.1;dbname=my_database', 'root', 'secret');
// $repository = new \App\Repository\TestMyTableRepository($pdo);

// Find by ID
$entity = $repository->find(1);

// Find all
$entities = $repository->findAll();

// Insert a new record
$entity = new \App\Entity\TestMyTable();
$entity->setName('John Doe');
$entity->setEmail('[email protected]');
$entity = $repository->insert($entity);
// $entity->getId() now contains the auto-generated ID

// Update an existing record
$entity->setName('Jane Doe');
$entity = $repository->update($entity);

// Delete by ID
$deleted = $repository->delete($entity->getId());

/**
 * Brief description of what the method does
 *
 * @param string $paramName Description of the parameter
 * @return Type Description of the return value
 * @throws RuntimeException When something goes wrong
 */
public function methodName(string $paramName): Type
{
    // Implementation
}

   private const array TYPE_MAP = [
       // ... existing mappings
       'new_sql_type' => 'php_type',
   ];