Symfony4:表单中的 Doctrine OneToMany 关系抛出错误 'Could not determine access type for property "X" in class "Y"'
Symfony4: Doctrine OneToMany relationship in a form throws error 'Could not determine access type for property "X" in class "Y"'
我有一个包含很多集合的父表单,但我想限制集合的数量,所以我创建了条件以将结果数量限制为 8
这对显示效果很好,但在编辑时效果不佳
我收到以下错误:
"Could not determine access type for property "Collections" in class
"App\Entity\Parent": Neither the property "Collections" nor one of the
methods "addCollections()"/"removeCollections()", "setCollections()",
"Collections()", "__set()" or "__call()" exist and have public access
in class "App\Entity\Parent"."
这可能是因为 $this->Collections->matching($criteria)
returns 一个 ArrayCollection
而不是默认的 PersistentCollection
。
我不知道如何从这里开始。
有人可以给我一些建议和参考吗?
/**
* @ORM\OneToMany(targetEntity="App\Entity\Collection", mappedBy="parent", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"createdOn" = "DESC"})
*/
private $Collections;
public function __construct()
{
$this->Collection; = new ArrayCollection();
}
public function getCollections(): Collection
{
$criteria = Criteria::create()
->setMaxResults(8);
return $this->Collections->matching($criteria);
}
你得到这个异常是因为 属性 $collections
是 private
(不是 public
)并且也没有 setter-方法。
这样表单组件就无法从外部设置 $collections
的值。
添加一个setCollections
方法如下:
/** @var Collection */
private $collections;
public function __construct()
{
$this->collections = new ArrayCollection();
}
public function setCollections(?Collection $collections)
{
foreach ($collections as $collection) {
$collection->setParent($this);
}
$this->collections = $collections;
}
然后在您的表单类型中为 collections
字段设置选项 'by_reference' => false
。
您可能想将 Collection
实体重命名为更有意义的名称,例如 Car
或 Picture
,并使用 $cars
或 $pictures
。 :)
进一步仔细检查 属性-名称以及大小写不匹配!
protected $collections
-> 小写
- 在
__construct
方法中:collections -> 小写,最后加"s".
我有一个包含很多集合的父表单,但我想限制集合的数量,所以我创建了条件以将结果数量限制为 8 这对显示效果很好,但在编辑时效果不佳
我收到以下错误:
"Could not determine access type for property "Collections" in class "App\Entity\Parent": Neither the property "Collections" nor one of the methods "addCollections()"/"removeCollections()", "setCollections()", "Collections()", "__set()" or "__call()" exist and have public access in class "App\Entity\Parent"."
这可能是因为 $this->Collections->matching($criteria)
returns 一个 ArrayCollection
而不是默认的 PersistentCollection
。
我不知道如何从这里开始。
有人可以给我一些建议和参考吗?
/**
* @ORM\OneToMany(targetEntity="App\Entity\Collection", mappedBy="parent", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"createdOn" = "DESC"})
*/
private $Collections;
public function __construct()
{
$this->Collection; = new ArrayCollection();
}
public function getCollections(): Collection
{
$criteria = Criteria::create()
->setMaxResults(8);
return $this->Collections->matching($criteria);
}
你得到这个异常是因为 属性 $collections
是 private
(不是 public
)并且也没有 setter-方法。
这样表单组件就无法从外部设置 $collections
的值。
添加一个setCollections
方法如下:
/** @var Collection */
private $collections;
public function __construct()
{
$this->collections = new ArrayCollection();
}
public function setCollections(?Collection $collections)
{
foreach ($collections as $collection) {
$collection->setParent($this);
}
$this->collections = $collections;
}
然后在您的表单类型中为 collections
字段设置选项 'by_reference' => false
。
您可能想将 Collection
实体重命名为更有意义的名称,例如 Car
或 Picture
,并使用 $cars
或 $pictures
。 :)
进一步仔细检查 属性-名称以及大小写不匹配!
protected $collections
-> 小写- 在
__construct
方法中:collections -> 小写,最后加"s".