英文:
Cannot assign Symfony\Component\VarDumper\Caster\CutStub to reference held by property App\Entity\AccountName::$accountType
问题
抱歉,以下是您提供的代码的翻译部分:
我正在使用Symfony 6.2开发项目。在这个项目中,有一个名为AccountType的实体,与另一个名为AccountCategory的实体具有多对一的关系。
AccountType实体:
#[ORM\Entity(repositoryClass: AccountTypeRepository::class)]
class AccountType
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $code = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $remarks = null;
#[ORM\OneToMany(mappedBy: 'accountType', targetEntity: AccountName::class)]
private Collection $accountNames;
#[ORM\ManyToOne(inversedBy: 'accountTypes')]
#[ORM\JoinColumn(nullable: false)]
private ?AccountCategory $accountCategory = null;
public function __construct()
{
$this->accountNames = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
// 其他方法略
}
另一个实体:
#[ORM\Entity(repositoryClass: AccountCategoryRepository::class)]
class AccountCategory
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'accountCategories')]
#[ORM\JoinColumn(nullable: false)]
private ?AccountReport $accountReport = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $code = null;
#[ORM\Column(length: 555, nullable: true)]
private ?string $remarks = null;
#[ORM\OneToMany(mappedBy: 'accountCategory', targetEntity: AccountType::class)]
private Collection $accountTypes;
public function __construct()
{
$this->accountTypes = new ArrayCollection();
}
// 其他方法略
}
关于您提到的错误,"Cannot assign Symfony\Component\VarDumper\Caster\CutStub to reference held by property App\Entity\AccountCategory::$accountReport of type ?App\Entity\AccountReport",这个错误可能与您的实体之间的关系或数据绑定有关。您可能需要仔细检查实体之间的关系和数据类型是否正确匹配,以解决这个错误。
英文:
I am developing a project by Symfony 6.2. In this project one entity AccountType with many to one relationship another entity AccountCategory.
#[ORM\Entity(repositoryClass: AccountTypeRepository::class)]
class AccountType
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $code = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $remarks = null;
#[ORM\OneToMany(mappedBy: 'accountType', targetEntity: AccountName::class)]
private Collection $accountNames;
#[ORM\ManyToOne(inversedBy: 'accountTypes')]
#[ORM\JoinColumn(nullable: false)]
private ?AccountCategory $accountCategory = null;
public function __construct()
{
$this->accountNames = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getRemarks(): ?string
{
return $this->remarks;
}
public function setRemarks(?string $remarks): self
{
$this->remarks = $remarks;
return $this;
}
/**
* @return Collection<int, AccountName>
*/
public function getAccountNames(): Collection
{
return $this->accountNames;
}
public function addAccountName(AccountName $accountName): self
{
if (!$this->accountNames->contains($accountName)) {
$this->accountNames->add($accountName);
$accountName->setAccountType($this);
}
return $this;
}
public function removeAccountName(AccountName $accountName): self
{
if ($this->accountNames->removeElement($accountName)) {
// set the owning side to null (unless already changed)
if ($accountName->getAccountType() === $this) {
$accountName->setAccountType(null);
}
}
return $this;
}
public function getAccountCategory(): ?AccountCategory
{
return $this->accountCategory;
}
public function setAccountCategory(?AccountCategory $accountCategory): self
{
$this->accountCategory = $accountCategory;
return $this;
}
}
Another Entity:
#[ORM\Entity(repositoryClass: AccountCategoryRepository::class)]
class AccountCategory
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'accountCategories')]
#[ORM\JoinColumn(nullable: false)]
private ?AccountReport $accountReport = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $code = null;
#[ORM\Column(length: 555, nullable: true)]
private ?string $remarks = null;
#[ORM\OneToMany(mappedBy: 'accountCategory', targetEntity: AccountType::class)]
private Collection $accountTypes;
public function __construct()
{
$this->accountTypes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getAccountReport(): ?AccountReport
{
return $this->accountReport;
}
public function setAccountReport(?AccountReport $accountReport): self
{
$this->accountReport = $accountReport;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getRemarks(): ?string
{
return $this->remarks;
}
public function setRemarks(?string $remarks): self
{
$this->remarks = $remarks;
return $this;
}
/**
* @return Collection<int, AccountType>
*/
public function getAccountTypes(): Collection
{
return $this->accountTypes;
}
public function addAccountType(AccountType $accountType): self
{
if (!$this->accountTypes->contains($accountType)) {
$this->accountTypes->add($accountType);
$accountType->setAccountCategory($this);
}
return $this;
}
public function removeAccountType(AccountType $accountType): self
{
if ($this->accountTypes->removeElement($accountType)) {
// set the owning side to null (unless already changed)
if ($accountType->getAccountCategory() === $this) {
$accountType->setAccountCategory(null);
}
}
return $this;
}
}
When data retrieve for editing of Account Type form then shown below error.
"Cannot assign Symfony\Component\VarDumper\Caster\CutStub to reference held by property App\Entity\AccountCategory::$accountReport of type ?App\Entity\AccountReport"
答案1
得分: 1
似乎与 PHP 版本相关的问题。我遇到了相同的问题,只需升级我的 PHP 到 8.2,问题就消失了。
另请参阅 --> https://github.com/symfony/symfony/issues/49091
英文:
Seems to be an issue related to php version. I had the same issue and just upgrade my php to 8.2 and the issue disapears.
See also --> https://github.com/symfony/symfony/issues/49091
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论