- Get link
- X
- Other Apps
add ini service.yml
and create file
doctrine.yml
selanjutnya buat file dan folder
namespace App\Doctrine\Connection;
buat file DynamicConnection.php
dan file DynamicConnectionCompilerPass.php
selanjutnya cara pemakaian dengan raw sql =
source : https://github.com/api-platform/api-platform/issues/556
app.event.authentication_success_listener: class: App\EventListener\AuthenticationSuccessListener tags: - { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse } # abc def ghi# ------------app.date_normalizer: class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer arguments: - { 'datetime_format': 'Y-m-d H:i:s' }
and create file
doctrine.yml
parameters: # Adds a fallback DATABASE_URL if the env var is not set. # This allows you to run cache:warmup even if your # environment variables are not available yet. # You should not need to change this value. env(DATABASE_URL): '' doctrine: dbal: default_connection: default connections: default: driver: 'pdo_sqlsrv' server_version : '11.0' url: '%env(resolve:DATABASE_URL)%' odsdapodik: driver: 'pdo_sqlsrv' server_version : '11.0' url: '%env(resolve:DATABASE_URL_ODS)%' # schema_filter: ~^(?!mst_)~ orm: auto_generate_proxy_classes: true naming_strategy: doctrine.orm.naming_strategy.underscore auto_mapping: true mappings: App: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App
selanjutnya buat file dan folder
namespace App\Doctrine\Connection;
buat file DynamicConnection.php
<?php namespace App\Doctrine\Connection; use Doctrine\DBAL\Connection;use Doctrine\DBAL\Driver;use Doctrine\DBAL\Event\ConnectionEventArgs;use Doctrine\DBAL\Events;use MyAwesome\Bundle\Services\DbSwitcher; class DynamicConnection extends Connection{/** @var DbSwitcher */protected $dbSwitcher; /*** @var bool*/private $_isConnected = false; /*** The parameters used during creation of the Connection instance.** @var array*/private $_params = array(); /*** @param DbSwitcher $dbSwitcher*/public function setDbSwitcher(DbSwitcher $dbSwitcher){$this->dbSwitcher = $dbSwitcher;} /*** @inheritDoc*/public function connect(){$container = $this->dbSwitcher->getContainer(); $connection = $container->get(sprintf('doctrine.dbal.sql.%s_connection', $this->dbSwitcher->getWorkingEntityManagerName())); $this->_params = $connection->getParams(); if ($this->_isConnected) {return false;} $driverOptions = isset($this->_params['driverOptions']) ?$this->_params['driverOptions'] : array();$user = isset($this->_params['user']) ? $this->_params['user'] : null;$password = isset($this->_params['password']) ?$this->_params['password'] : null; $this->_conn = $this->_driver->connect($this->_params, $user, $password, $driverOptions);$this->_isConnected = true; if (false === $this->isAutoCommit()) {$this->beginTransaction();} if ($this->_eventManager->hasListeners(Events::postConnect)) {$eventArgs = new ConnectionEventArgs($this);$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);} return true;}}
dan file DynamicConnectionCompilerPass.php
<?phpnamespace App\Doctrine\DependencyInjection\CompilerPass; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;use Symfony\Component\DependencyInjection\ContainerBuilder;use Symfony\Component\DependencyInjection\Reference; class DynamicConnectionCompilerPass implements CompilerPassInterface{ /** * @inheritDoc */ public function process(ContainerBuilder $container) { $container ->getDefinition('doctrine.dbal.sql.dynamic_connection') ->addMethodCall('setDbSwitcher', [ new Reference('myawesomebundle.dbswitcher') ]); } }
selanjutnya cara pemakaian dengan raw sql =
$connOds = $this->managerRegistry->getConnection('odsdapodik'); $sql = "select top 1 * from ptk with(NOLOCK) where nik='".$varRequest->nik."'";$stmt = $connOds->prepare($sql);$stmt->execute();selecsai
source : https://github.com/api-platform/api-platform/issues/556
Comments
Post a Comment