PHP code example of tourze / doctrine-resolve-target-entity-bundle

1. Go to this page and download the library: Download tourze/doctrine-resolve-target-entity-bundle 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/ */

    

tourze / doctrine-resolve-target-entity-bundle example snippets


return [
    // ...
    Tourze\DoctrineResolveTargetEntityBundle\DoctrineResolveTargetEntityBundle::class => ['all' => true],
];



namespace App\Entity;

interface UserInterface
{
    public function getId(): int;
    public function getName(): string;
}



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class User implements UserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private int $id;

    #[ORM\Column(type: 'string')]
    private string $name;

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

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



namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Tourze\DoctrineResolveTargetEntityBundle\DependencyInjection\Compiler\ResolveTargetEntityPass;
use App\Entity\UserInterface;
use App\Entity\User;

class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container): void
    {
        $container->addCompilerPass(new ResolveTargetEntityPass(
            UserInterface::class,
            User::class
        ));
    }
}



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Article
{
    #[ORM\ManyToOne(targetEntity: UserInterface::class)]
    private UserInterface $author;

    public function getAuthor(): UserInterface
    {
        return $this->author;
    }
}

use Tourze\DoctrineResolveTargetEntityBundle\Service\ResolveTargetEntityService;

class MyService
{
    public function __construct(
        private ResolveTargetEntityService $resolveTargetEntityService
    ) {}

    public function findEntityClass(string $interface): string
    {
        return $this->resolveTargetEntityService->findEntityClass($interface);
    }
}

// Multiple mappings
$container->addCompilerPass(new ResolveTargetEntityPass(
    'App\Entity\UserInterface',
    'App\Entity\User'
));

$container->addCompilerPass(new ResolveTargetEntityPass(
    'App\Entity\ProductInterface',
    'App\Entity\Product'
));