admin管理员组文章数量:1196766
I am quite new with Simfony and I'm having trouble configuring api platform to search a specific database when accessing an endpoint.
I tested that the connections with the db are working with the simfony console commands but when I access the endpoint that corresponds to an entity of the secondary database in the logs I see that it continues trying to use the db that I configured for the first connection
I have the following configuration done so far.
I am using symfony 3.4, api platform 2.2 and doctrine 2.6.3
# Doctrine Configuration
doctrine:
dbal:
connections:
main:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
secondary:
driver: pdo_mysql
host: '%secondary_database_host%'
port: '%secondary_database_port%'
dbname: '%secondary_database_name%'
user: '%secondary_database_user%'
password: '%secondary_database_password%'
charset: UTF8
orm:
entity_managers:
default:
connection: main
mappings:
AppBundle:
type: annotation
dir: '%kernel.project_dir%/src/AppBundle/Entity'
prefix: AppBundle\Entity
secondary:
mappings:
AppBundle\Entity\Secondary\:
type: annotation
is_bundle: false
dir: '%kernel.project_dir%/src/AppBundle/Entity/Secondary'
prefix: AppBundle\Entity\Secondary
In the services.yml create the following
services:
AppBundle\DataProvider\DynamicEntityManagerDataProvider:
arguments:
$mainEntityManager: '@doctrine.orm.entity_managers.main'
$secondaryEntityManager: '@doctrine.orm.entity_managers.secondary'
tags:
- { name: 'api_platform.collection_data_provider' }
- { name: 'api_platform.item_data_provider' }
AppBundle\DataPersister\DynamicEntityManagerDataPersister:
arguments:
$mainEntityManager: '@doctrine.orm.main_entity_manager'
$secondaryEntityManager: '@doctrine.orm.secondary_entity_manager'
tags: ['api_platform.data_persister']
And in the Data provider I have this
<?php
namespace AppBundle\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Doctrine\ORM\EntityManagerInterface;
class DynamicEntityManagerDataProvider implements
CollectionDataProviderInterface,
ItemDataProviderInterface,
RestrictedDataProviderInterface
{
private $defaultEntityManager;
private $secondaryEntityManager;
public function __construct(EntityManagerInterface $defaultEntityManager, EntityManagerInterface $secondaryEntityManager)
{
$this->defaultEntityManager = $defaultEntityManager;
$this->secondaryEntityManager = $secondaryEntityManager;
}
public function getCollection(string $resourceClass, string $operationName = null)
{
$entityManager = $this->getEntityManagerForResource($resourceClass);
return $entityManager->getRepository($resourceClass)->findAll();
}
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
$entityManager = $this->getEntityManagerForResource($resourceClass);
return $entityManager->getRepository($resourceClass)->find($id);
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
$supportedEntities = [
\AppBundle\Entity\Secondary\SecondaryCustomer::class,
// Agrega más entidades según sea necesario
];
return in_array($resourceClass, $supportedEntities, true);
}
private function getEntityManagerForResource(string $resourceClass): EntityManagerInterface
{
if (strpos($resourceClass, 'Secondary')) {
dump('Using secondary entity manager for:', $resourceClass);
return $this->secondaryEntityManager;
}
return $this->defaultEntityManager;
}
}
本文标签: phphow to work with multiples databases in differents servers api platformStack Overflow
版权声明:本文标题:php - how to work with multiples databases in differents servers api platform - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738543638a2095986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论