PHP code example of stargazer-team / yii-doctrine

1. Go to this page and download the library: Download stargazer-team/yii-doctrine 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/ */

    

stargazer-team / yii-doctrine example snippets




declare(strict_types=1);

use Yiisoft\Yii\Doctrine\Dbal\Enum\ConfigOptions;
use Yiisoft\Yii\Doctrine\Dbal\Factory\DynamicConnectionFactory;
use Yiisoft\Yii\Doctrine\DoctrineManager;

final class ConnectionService
{
    public function __construct(
        private readonly DynamicConnectionFactory $dynamicConnectionFactory,
        private readonly DoctrineManager $doctrineManager,
    ) {
    }
    
    public function create(): void
    {
        $connectionModel = $this->dynamicConnectionFactory->createConnection(
            [
                ConfigOptions::PARAMS => [
                    'driver' => 'pdo_pgsql',
                    'dbname' => 'dbname',
                    'host' => 'localhost',
                    'password' => 'secret',
                    'user' => 'postgres',
                ]
            ],
            'postgres'
        );
    }
    
    public function close(): void
    {
        $this->doctrineManager->closeConnection('postgres');
    }
}



declare(strict_types=1);

use Yiisoft\Yii\Doctrine\DoctrineManager
use Yiisoft\Yii\Doctrine\Orm\Enum\ConfigOptions;

EntityManagerInterface::class => fn(
    DoctrineManager $doctrineManager
): EntityManagerInterface => $doctrineManager->getManager(
        $params['yiisoft/yii-doctrine'][ConfigOptions::ORM][ConfigOptions::DEFAULT_ENTITY_MANAGER] ?? DoctrineManager::DEFAULT_ENTITY_MANAGER,
   ),



declare(strict_types=1);

use Doctrine\ORM\EntityManagerInterface;

final class TestController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {
    }
}



declare(strict_types=1);

use Yiisoft\Yii\Doctrine\DoctrineManager;

final class Test2Controller
{
    public function __construct(
        private readonly DoctrineManager $doctrineManager,
    ) {
    }
}



declare(strict_types=1);

use Yiisoft\Yii\Doctrine\DoctrineManager;
use Yiisoft\Yii\Doctrine\Orm\Enum\ConfigOptions;
use Yiisoft\Yii\Doctrine\Orm\Factory\DynamicEntityManagerFactory;

final class EntityManagerService
{
    public function __construct(
        private readonly DynamicEntityManagerFactory $dynamicEntityManagerFactory,
        private readonly DoctrineManager $doctrineManager,
    ) {
    }
    
    public function create(): void
    {
        $this->dynamicEntityManagerFactory->create(
            [
                ConfigOptions::CONNECTION => 'mysql',
                ConfigOptions::MAPPINGS => [
                    'Mysql' => [
                        ConfigOptions::MAPPING_DIR => '@common/Mysql',
                        ConfigOptions::MAPPING_DRIVER => DriverMappingEnum::ATTRIBUTE_MAPPING,
                        ConfigOptions::MAPPING_NAMESPACE => 'Common\Mysql',
                    ],
                ],
            ],
            [
                ConfigOptions::PROXY_NAMESPACE => 'Proxies',
                ConfigOptions::PROXY_PATH => '@runtime/cache/doctrine/proxy',
                ConfigOptions::PROXY_AUTO_GENERATE => true
            ],
            'mysql'
        );

        $entityManager = $this->doctrineManager->getManager('mysql');
    }
    
    public function reset(): void
    {
        $this->doctrineManager->resetManager('mysql');
    }
    
    public function close(): void
    {
        $this->doctrineManager->closeManager('mysql');
    }
}



declare(strict_types=1);

use Yiisoft\Yii\Doctrine\Cache\CacheCollector;
use Yiisoft\Yii\Doctrine\Cache\Enum\ConfigOptions;

return [
    'yiisoft/yii-doctrine' => [
        // Used symfony cache
        ConfigOptions::CACHE => [
            ConfigOptions::DRIVER => CacheAdapterEnum::ARRAY_ADAPTER,
            // only redis or memcached
            ConfigOptions::SERVER => [
                ConfigOptions::HOST => 'localhost',
                ConfigOptions::PORT => 6379
            ],
            ConfigOptions::NAMESPACE => 'doctrine_',
            // only file cache driver
            ConfigOptions::PATH => '@runtime/cache/doctrine',
        ],
];



declare(strict_types=1);

use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Yiisoft\Yii\Doctrine\Cache\CacheCollector

return [
    CacheCollector::DOCTRINE_HYDRATION_CACHE => fn(CacheFactory $cacheFactory): CacheItemPoolInterface => $cacheFactory->create(
        $params['yiisoft/yii-doctrine']['cache'] ?? []
    ),
    // or add di.php customer implementation
    CacheCollector::DOCTRINE_HYDRATION_CACHE => ArrayAdapter(),        
    CacheCollector::DOCTRINE_METADATA_CACHE => ArrayAdapter(),        
    CacheCollector::DOCTRINE_QUERY_CACHE => ArrayAdapter(),        
    CacheCollector::DOCTRINE_RESULT_CACHE => ArrayAdapter(),        
    
];

bash
php yii doctrine:database:create
bash
php yii doctrine:migrations:diff
bash
php yii doctrine:migrations:diff --configuration=mysql
bash
php yii doctrine:migrations:migrate