需要连接到多个数据库,并遵循Symfony文档中关于此问题的说明。
我创建了多个条令连接和orm实体管理器,并禁用了自动布线。
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: "%env(resolve:DATABASE_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
lc_cvo:
# configure these for your database server
url: "%env(resolve:DATABASE_LC_CVO_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
lc_cvt:
# configure these for your database server
url: "%env(resolve:DATABASE_LC_CVT_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
lc_ewi:
# configure these for your database server
url: "%env(resolve:DATABASE_LC_EWI_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
lc_tbo:
# configure these for your database server
url: "%env(resolve:DATABASE_LC_TBO_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
lc_users:
# configure these for your database server
url: "%env(resolve:DATABASE_LC_USERS_URL)%"
driver: "pdo_mysql"
server_version: "5.7"
charset: utf8mb4
orm:
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_lemoncake"
prefix: 'App\Entity\lmc_lemoncake'
alias: Main
lc_cvo:
connection: lc_cvo
mappings:
lc_cvo:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_lemoncake-cvo"
prefix: 'App\Entity\lmc_lemoncake_cvo'
alias: lc_cvo
lc_cvt:
connection: lc_cvt
mappings:
lc_cvt:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_lemoncake-cvt"
prefix: 'App\Entity\lmc_lemoncake_cvt'
alias: lc_cvt
lc_ewi:
connection: lc_ewi
mappings:
lc_ewi:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_lemoncake-ewi"
prefix: 'App\Entity\lmc_lemoncake_ewi'
alias: lc_ewi
lc_tbo:
connection: lc_tbo
mappings:
lc_tbo:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_lemoncake-tbo"
prefix: 'App\Entity\lmc_lemoncake_tbo'
alias: lc_tbo
lc_users:
connection: lc_users
mappings:
lc_users:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_users"
prefix: 'App\Entity\lmc_users'
alias: lc_users
我的services.yaml文件如下所示
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: false # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: "../src/"
exclude:
- "../src/DependencyInjection/"
- "../src/Entity/"
- "../src/Kernel.php"
- "../src/Tests/"
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: "../src/Controller/"
tags: ["controller.service_arguments"]
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
不幸的是,我在尝试访问登录页面时收到以下错误。
Too few arguments to function App\Security\LoginFormAuthenticator::__construct(), 0 passed in /var/www/html/app/var/cache/dev/Container12fc4el/getSecurity_Firewall_Map_Context_MainService.php on line 53 and exactly 4 expected
此处列出了它所指的LoginFormAuthenticator,它需要连接到用户信息(用户名,pw)所在的lc_用户连接。我将需要其他连接来获取客户端数据。
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private $entityManager;
private $urlGenerator;
private $csrfTokenManager;
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
}
我相信我需要添加一些东西到我的服务,以便身份验证器可以检索正确的连接,不幸的是,我对此事的知识是不够的。
我需要为多个客户端使用多个数据库。
提前感谢您的帮助,请随时询问更多信息。
编辑:感谢@msg的回答;我已经设法通过以下代码使其工作:
app/配置/services.yaml:
App\Security\LoginFormAuthenticator:
autowire: true
tags: ["doctrine.repository_service"]
arguments:
$entityManager: "@doctrine.orm.lc_users_entity_manager"
app/config/doctrine.yaml:
orm:
default_entity_manager: default
entity_managers:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
default:
...
lc_users:
connection: lc_users
mappings:
App\Entity\lmc_lemoncake:
is_bundle: false
type: annotation
dir: "%kernel.project_dir%/src/Entity/lmc_users"
prefix: 'App\Entity\lmc_users'
alias: lc_users
(部分)LoginFormAuthenticator的getUser函数:
$em = $this->entityManager;
$repo = $em->getRepository(Users::class, 'lc_users');
$user = $repo->findOneBy(['username' => $credentials['username']]);
如前所述,注入EntityManager
将获得默认值。条令将为每个经理注册一个名为
条令.orm的服务。%manager\u name%\u entity\u manager
。
所以如果你需要一个不同的,你有几个选择:
1.明确配置服务以使用所需的管理器:
services:
App\Security\LoginFormAuthenticator:
arguments:
# Override the Manager, other arguments will be autowired.
$entityManager: '@doctrine.orm.lc_users_entity_manager'
2.为不同的实体管理器创建不同的绑定:
_defauts:
bind:
$cvoManager: '@doctrine.orm.lc_cvo_entity_manager'
$usersManager: '@doctrine.orm.lc_users_entity_manager'
# Other managers...
根据依赖项上的变量名,将注入认可的管理器:
public function __construct(
EntityManagerInterface $entityManager, // Default manager
EntityManagerInterface $usersManager,
)
3.如果有许多依赖项,还可以注入
ManagerRegistry
(调用getdoctor()
时在AbstractController
中获得的对象)。它充当服务定位器,从中可以检索管理器或存储库:
use Doctrine\Persistence\ManagerRegistry;
public function __construct(ManagerRegistry $registry) {
$userManager = $registry->getManager('users');
}
如果您有许多具有不同依赖关系的服务,那么第一个选项可能会很快变得笨拙,而第三个选项将掩盖您真正的依赖关系,并在您需要模拟注册表时使测试复杂化。
我试图在我的services.yaml中为ImageManager.php获取一个参数,但它不起作用,我无法解决此错误。 错误是这样的: 类型错误:参数太少,无法运行App\Manager\ImageManager::__construct(),0在C:\wamp64\www\SymfonyAPI\var\ache\dev\ContainerZxFSS5S\getImageManagerServi
我是PHP和Laravel的初学者。我在我的项目Laravel 7中使用。我有存储库模式在我的项目与缓存。 PageServiceProvider: CachingBaseRepository: BaseRepository: PageRepository: 恶作剧记录片 PageRepositoryInterface: 我想在上面的代码中为我的网站添加缓存。我的控制器如下所示: 当我运行上述代码
我得到以下错误: 致命错误:Uncaught ArgumentCounter错误:参数太少,无法使用wpdb::prepare()函数,第635行的/home/admin/public_html/ocarinatab.com/wp-content/plugins/ocarina-tabs/ocarina-tabs.php中传递了1个,在/home/admin/public_html/ocarina
在我的symfony4项目中,我在/src/Blocks/Hello/HelloBlock中创建了一个HelloBlock类。php文件。 这是它的构造函数... 然后在我的services.yaml我添加了这个... 运行代码(开发环境、缓存清除等)时,出现“参数太少”错误。这不是注入依赖性。 有人能帮忙吗?我以为这就是Symfony DI应该做的。 谢谢
实际错误消息是 我在新PC(机器)中设置了我现有的Laravel项目。但是,完成所有过程后,由于上面提到的错误,我无法运行项目。
单击视图上的“销售”按钮后出现错误 错误: 遇到未捕获的异常 类型:ArgumentCountError 消息:太少的参数功能猫100::销售(),0传递在C:\xampp\htdocs\catshop 100\system\core\CodeIgniter.php在第532行,正好1预期 文件名:C:\xampp\htdocs\catshop100\application\controllers