检测到服务的循环引用
Circular reference detected for service
我有一个简单的 class,看起来像这样:
<?php
namespace App\Algorithm;
use App\Dao\MatchDao;
use App\Service\MatchService;
class Calculator {
private $users;
private $matchDao;
function __construct(MatchService $matchService, MatchDao $matchDao) {
$this->users = $matchService->users;
$this->matchDao = $matchDao;
}
public function hourlyRate() {
$query = $this->matchDao->getSingleColumn('Payment', 'hourly_rate', 32);
var_dump($query);
}
}
但我收到以下错误消息:
Circular reference detected for service "App\Algorithm\Calculator",
path: "App\Algorithm\Calculator -> App\Service\MatchService ->
App\Algorithm\Calculator".
MatchService.php
<?php
namespace App\Service;
use App\Algorithm\Calculator;
use App\Algorithm\Collection;
class MatchService {
public $users;
private $collection;
private $calculator;
function __construct(Collection $collection, Calculator $calculator) {
$this->collection = $collection;
$this->calculator = $calculator;
}
public function getMatch($data) {
$this->users = $this->collection->getAllUsers($data);
$this->calculator->hourlyRate();
return 1;
}
}
问题是 MatchService
但我到底做错了什么?
这通常发生在 classes 相互依赖注入时,因此循环引用。
给你上面的例子,你的 class MatchService
注入 Collection
和 Calculator
。其中之一(假设计算器作为集合可能是一个学说 class)依赖注入你的 MatchService
.
我认为您的 class 是这样的:
class MatchService
{
public $users;
private $collection;
private $calculator;
public function __construct(Collection $collection, Calculator $calculator) {
$this->collection = $collection;
$this->calculator = $calculator;
}
}
class Calculator
{
private $matchService;
public function __construct(MatchService $matchService)
{
$this->matchService = $matchService;
}
}
您有两个选择:
- 更多服务,更少依赖
- 使用静力学
我们很难为您解决,因为这取决于您构建应用程序的方式。
正如一些人指出的那样,循环依赖来自于您试图将计算器注入 MatchService 的同时,将 MatchService 注入计算器。无法先创建一个再创建另一个。
再深入一点,Calculator 似乎正在使用 MatchService 获取用户列表。作为第二个问题,Calculator 试图在 MatchService 生成用户之前获取用户。
这是一种可能的重构:
class Calculator
{
private $matchDao;
public function __construct(MatchDao $matchDao)
{
$this->matchDao = $matchDao;
}
public function getHourlyRate($users) // Added argument
{
$query = $this->matchDao->getSingleColumn('Payment', 'hourly_rate', 32);
}
}
class MatchService
{
private $collection;
private $calculator;
public function __construct(Collection $collection, Calculator $calculator)
{
$this->calculator = $calculator;
$this->collection = $collection;
}
public function getMatch($data)
{
$users = $this->collection->getAllUsers($data);
$this->calculator->getHourlyRate($users);
}
}
从计算器的构造函数中删除 MatchService 解决了循环依赖问题。将 $users 传递给 getHourlyRate 解决了在用户可用之前尝试获取用户的问题。
这当然只是一种可能的解决方案。从您发布的代码中不清楚计算器是否真的需要 $users。
很明显,您正在将服务 A 注入服务 B,同时将服务 B 注入服务 A。
这样做似乎有点不合逻辑,但有时是需要的。
就我而言,我有两项服务:
_MySesion -> 哪些原型是 Symfony Session
_MyClient -> 负责识别客户端并获取其数据库凭证
我使用 MySession 来存储这些凭据,因此整个系统都可以使用它,但是,要使用 MyClient 获取这些凭据,我需要将一些信息存储到 MySession ....看,两个服务需要彼此工作...
我开始看到同样的东西
Circular reference detected for service
刚刚升级到 Symfony 5。并且,sfy5 本身提出了解决方案:
composer require symfony/proxy-manager-bridge
请记住,服务可以设置为
lazy : true
有关 Symfony Docs
的更多信息
我有一个简单的 class,看起来像这样:
<?php
namespace App\Algorithm;
use App\Dao\MatchDao;
use App\Service\MatchService;
class Calculator {
private $users;
private $matchDao;
function __construct(MatchService $matchService, MatchDao $matchDao) {
$this->users = $matchService->users;
$this->matchDao = $matchDao;
}
public function hourlyRate() {
$query = $this->matchDao->getSingleColumn('Payment', 'hourly_rate', 32);
var_dump($query);
}
}
但我收到以下错误消息:
Circular reference detected for service "App\Algorithm\Calculator", path: "App\Algorithm\Calculator -> App\Service\MatchService -> App\Algorithm\Calculator".
MatchService.php
<?php
namespace App\Service;
use App\Algorithm\Calculator;
use App\Algorithm\Collection;
class MatchService {
public $users;
private $collection;
private $calculator;
function __construct(Collection $collection, Calculator $calculator) {
$this->collection = $collection;
$this->calculator = $calculator;
}
public function getMatch($data) {
$this->users = $this->collection->getAllUsers($data);
$this->calculator->hourlyRate();
return 1;
}
}
问题是 MatchService
但我到底做错了什么?
这通常发生在 classes 相互依赖注入时,因此循环引用。
给你上面的例子,你的 class MatchService
注入 Collection
和 Calculator
。其中之一(假设计算器作为集合可能是一个学说 class)依赖注入你的 MatchService
.
我认为您的 class 是这样的:
class MatchService
{
public $users;
private $collection;
private $calculator;
public function __construct(Collection $collection, Calculator $calculator) {
$this->collection = $collection;
$this->calculator = $calculator;
}
}
class Calculator
{
private $matchService;
public function __construct(MatchService $matchService)
{
$this->matchService = $matchService;
}
}
您有两个选择:
- 更多服务,更少依赖
- 使用静力学
我们很难为您解决,因为这取决于您构建应用程序的方式。
正如一些人指出的那样,循环依赖来自于您试图将计算器注入 MatchService 的同时,将 MatchService 注入计算器。无法先创建一个再创建另一个。
再深入一点,Calculator 似乎正在使用 MatchService 获取用户列表。作为第二个问题,Calculator 试图在 MatchService 生成用户之前获取用户。
这是一种可能的重构:
class Calculator
{
private $matchDao;
public function __construct(MatchDao $matchDao)
{
$this->matchDao = $matchDao;
}
public function getHourlyRate($users) // Added argument
{
$query = $this->matchDao->getSingleColumn('Payment', 'hourly_rate', 32);
}
}
class MatchService
{
private $collection;
private $calculator;
public function __construct(Collection $collection, Calculator $calculator)
{
$this->calculator = $calculator;
$this->collection = $collection;
}
public function getMatch($data)
{
$users = $this->collection->getAllUsers($data);
$this->calculator->getHourlyRate($users);
}
}
从计算器的构造函数中删除 MatchService 解决了循环依赖问题。将 $users 传递给 getHourlyRate 解决了在用户可用之前尝试获取用户的问题。
这当然只是一种可能的解决方案。从您发布的代码中不清楚计算器是否真的需要 $users。
很明显,您正在将服务 A 注入服务 B,同时将服务 B 注入服务 A。 这样做似乎有点不合逻辑,但有时是需要的。 就我而言,我有两项服务:
_MySesion -> 哪些原型是 Symfony Session
_MyClient -> 负责识别客户端并获取其数据库凭证
我使用 MySession 来存储这些凭据,因此整个系统都可以使用它,但是,要使用 MyClient 获取这些凭据,我需要将一些信息存储到 MySession ....看,两个服务需要彼此工作...
我开始看到同样的东西
Circular reference detected for service
刚刚升级到 Symfony 5。并且,sfy5 本身提出了解决方案:
composer require symfony/proxy-manager-bridge
请记住,服务可以设置为
lazy : true
有关 Symfony Docs
的更多信息