[英]Strange circular dependency in Symfony 5
I am having a problem with Symfony 5 and its Circular Dependency detection mechanism.我在使用 Symfony 5 及其循环依赖检测机制时遇到问题。
I have defined the following class我已经定义了以下类
namespace App\HTSC\Entity\Finance\Subscription;
/**
* Category
*/
class Category {
/**
* @var string
*/
private $name;
/**
* @var integer
*/
private $version = 1;
/**
* @var integer
*/
private $id;
/**
* @var array
*/
private $fees;
/**
* Map of feeType => fee
* @var array
*/
private $feeMap = null;
/**
* Constructor
*/
public function __construct(Category $category = null)
{
if (!is_null($category)) {
$this->name = $category->name;
$this->setFees($category->fees);
} else {
$this->fees = array();
}
}
/**
* Set name
*
* @param string $name
* @return Category
* @throws \InvalidArgumentException
*/
public function setName($name)
{
if (is_null($name) || empty($name = trim($name))) {
return \InvalidArgumentException('Empty category name');
}
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set version
*
* @param integer $version
* @return Category
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* @return integer
*/
public function getVersion()
{
return $this->version;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Sets an array of fees to the category.
*
* @param array $fees
* @return Category
*/
public function setFees(array $fees) {
$this->fees = array();
$this->feeMap = array();
foreach ($fees as $fee) {
$this->addFee($fee);
}
return $this;
}
/**
* Add fee to category. Only one fee per fee type allowed.
*
* @param Fee $fee
* @return Category
*/
public function addFee(Fee $fee)
{
if (!$fee->isValid()) throw new \InvalidArgumentException('Invalid fee added to category');
$feeMap = $this->getFeeMap();
if (key_exists($fee->getType()->getName(), $feeMap)) {
throw new \InvalidArgumentException('Multiple fee type [' . $fee->getType()->getName() . '] specified for category [' . $this->name . ']');
}
$this->fees[] = $fee;
$this->feeMap[$fee->getType()->getName()] = $fee;
return $this;
}
/**
* Remove fee
*
* @param Fee $fee
* @return Category
*/
public function removeFee(Fee $fee)
{
$this->fees->removeElement($fee);
return $this;
}
/**
* Get fees
*
* @return Fee[]
*/
public function getFees()
{
return $this->fees;
}
/**
* Get fee of given type. Returns null if type is not present.
*
* @return Fee
*/
public function getFee(FeeType $type)
{
$feeMap = $this->getFeeMap();
if (key_exists($type->getName(), $feeMap)) return $feeMap[$type->getName()];
return null;
}
private function getFeeMap() {
if (is_null($this->feeMap)) {
$this->feeMap = array();
foreach ($this->fees as $fee) $this->feeMap[$fee->getType()->getName()] = $fee;
}
return $this->feeMap;
}
/**
* Returns whether a category is considered valid.
* @return boolean
*/
public function isValid() {
if (empty($this->name)) return false;
if (empty($this->fees)) return false;
return true;
}
}
When I access the web site and hence my code above, Symfony responds with the following message:当我访问该网站以及我上面的代码时,Symfony 响应以下消息:
Circular reference detected for service "App\HTSC\Entity\Finance\Subscription\Category", path: "App\HTSC\Entity\Finance\Subscription\Category -> App\HTSC\Entity\Finance\Subscription\Category".
Normally when a circular dependency is detected, a second class is involved;通常当检测到循环依赖时,会涉及到第二类; in my case it is a circular dependency between the class itself?
在我的情况下,它是类本身之间的循环依赖?
I also inspected other classes using this class, but cannot determine a circular dependency with the this class anywhere.我还使用此类检查了其他类,但无法在任何地方确定与此类的循环依赖关系。
Anybody experienced the same problem and knows a solution for this?任何人都遇到过同样的问题并知道解决方案吗?
If that is a model or entity it should not be registered as a service.如果这是一个模型或实体,则不应将其注册为服务。
Since it is registered as a service then it attempts to set Category
service to the Category
in the constructor:由于它已注册为服务,因此它尝试将
Category
服务设置为构造函数中的Category
:
public function __construct(Category $category = null)
If you have explicitly registered this as a service in your services.yaml
then please unregister it.如果您已在
services.yaml
明确将此注册为服务,请取消注册。
If you have not registered it explicitly then please exclude it in your automatic wiring.如果您没有明确注册,那么请在您的自动布线中排除它。 For example if you have an entry:
例如,如果您有一个条目:
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
then be sure to add the folder which holds your class to the exclude
value.然后确保将包含您的类的文件夹添加到
exclude
值。
Please note that "normal" (root level) Entities
are also excluded here.请注意,此处也不包括“正常”(根级别)
Entities
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.