klsoft/yii3-doctrine

The package provides an easy way to integrate the Yii 3 framework with the Doctrine ORM.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/klsoft/yii3-doctrine

1.0.0 2026-02-14 11:26 UTC

This package is auto-updated.

Last update: 2026-02-14 11:28:27 UTC


README

The package provides an easy way to integrate the Yii 3 framework with the Doctrine ORM.

Requirement

  • PHP 8.2 or higher.

Installation

composer require klsoft/yii3-doctrine

How to use

1. Configure the EntityManagerInterface.

Add the Doctrine parameters to the config/common/params.php:

return [
    // ...
    'doctrine' => [
        'paths' =>  [__DIR__ . '/../../src/Data/Entities'],
        'isDevMode' => true,
        'connection' => [
            'driver'   => 'pdo_mysql',
            'user'     => 'mydb',
            'password' => 'secret',
            'dbname'   => 'mydb',
        ]
    ],
];

Register the Doctrine dependencies in the config/common/di/application.php:

use Doctrine\ORM\ORMSetup;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use Psr\Container\ContainerInterface;

return [
    // ...
    Configuration::class => ORMSetup::createAttributeMetadataConfiguration(
        paths: $params['doctrine']['paths'],
        isDevMode: $params['doctrine']['isDevMode']
    ),
    EntityManagerInterface::class => static function (ContainerInterface $container) use ($params) {
        $configuration = $container->get(Configuration::class);
        return new EntityManager(
            DriverManager::getConnection(
                $params['doctrine']['connection'],
                $configuration
            ),
            $configuration);
    },
    EntityManagerProvider::class => SingleManagerProvider::class,
];

2. Run the Doctrine console command.

Example:

APP_ENV=dev ./yii doctrine:orm:schema-tool:create

The following commands are currently available:

  • doctrine:orm:schema-tool:create
  • doctrine:orm:schema-tool:drop
  • doctrine:orm:schema-tool:update
  • doctrine:orm:clear-cache:metadata
  • doctrine:orm:validate-schema
  • doctrine:orm:mapping-describe
  • doctrine:orm:run-dql
  • doctrine:orm:info
  • doctrine:orm:generate-proxies
  • doctrine:orm:clear-cache:query
  • doctrine:orm:clear-cache:result
  • doctrine:dbal:run-sql

3. Inject the EntityManagerInterface.

Example:

use Doctrine\ORM\EntityManagerInterface;

final class ProductRepository implements ProductRepositoryInterface
{
    public function __construct(private EntityManagerInterface $entityManager)
    {
    }
}