如何访问我的数组 (Symfony 4) 中的 ArrayCollection 数据?
How can I get access to ArrayCollection data inside my Array (Symfony 4)?
fields table
:
productgroup table
:
在我的控制器中,我像这样加载 fields_array
:
$fields_array = $this->getDoctrine()->getRepository(class::fields)->findAll();
如果字段和产品组没有连接,我的 fields_array
看起来像这样:
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: []
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: []
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7545 ▶}
}
]
如您所见,ArrayCollecton
不包含任何元素。
现在我将 cat
连接到产品组 Animals
。所以我的 table fields_productgroup
看起来像这样:
现在如您所见,我的 ArrayCollection
of cat
包含元素 Animals
:
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: array:1 [▼
0 => Productgroup {#7146 ▼
-id: 6
-name: "Animals"
-unique_id: "9e4ef1c46f"
-fields: PersistentCollection {#7357 ▶}
}
]
}
#initialized: true
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: []
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7545 ▶}
}
]
现在我也将 horse
连接到产品组 Animals
。所以我的 table fields_productgroup
看起来像这样:
My fields_array
for horse
显示,ArrayCollection
中有一个元素,但它不包含 Animals
的信息。它只是一个空数组......但我实际上需要连接到哪个产品组马的信息
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: array:1 [▼
0 => Productgroup {#7146 ▼
-id: 6
-name: "Animals"
-unique_id: "9e4ef1c46f"
-fields: PersistentCollection {#7357 ▶}
}
]
}
#initialized: true
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: array:1 [▼
0 => Productgroup {#7146}
]
}
#initialized: true
}
-type: Type {#7545 ▶}
}
]
我的字段实体:
/**
* @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields")
* @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
*/
private $productgroup;
public function getProductgroup()
{
return $this->productgroup;
}
public function setProductgroup($productgroup): self
{
$this->productgroup = $productgroup;
return $this;
}
public function __construct()
{
$this->productgroup = new ArrayCollection();
}
首先,我认为您将 PHP 对象与 PHP 数组混淆了。但是从您的屏幕截图来看,您似乎已经得到了您想要的东西,"horse" 对象的 productgroup
关联已完全加载。
这可以在您的转储中观察到:
正如所料,猫和马对象的相关 Productgroup 对象的 uid 都是#7146,因为它们与同一产品组相关。
我认为您只是被 horse 的 Productgroup 在您的转储中不可扩展这一事实误导了。
fields table
:
productgroup table
:
在我的控制器中,我像这样加载 fields_array
:
$fields_array = $this->getDoctrine()->getRepository(class::fields)->findAll();
如果字段和产品组没有连接,我的 fields_array
看起来像这样:
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: []
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: []
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7545 ▶}
}
]
如您所见,ArrayCollecton
不包含任何元素。
现在我将 cat
连接到产品组 Animals
。所以我的 table fields_productgroup
看起来像这样:
现在如您所见,我的 ArrayCollection
of cat
包含元素 Animals
:
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: array:1 [▼
0 => Productgroup {#7146 ▼
-id: 6
-name: "Animals"
-unique_id: "9e4ef1c46f"
-fields: PersistentCollection {#7357 ▶}
}
]
}
#initialized: true
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: []
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: []
}
#initialized: false
}
-type: Type {#7545 ▶}
}
]
现在我也将 horse
连接到产品组 Animals
。所以我的 table fields_productgroup
看起来像这样:
My fields_array
for horse
显示,ArrayCollection
中有一个元素,但它不包含 Animals
的信息。它只是一个空数组......但我实际上需要连接到哪个产品组马的信息
array:2 [▼
0 => Fields {#7460 ▼
-id: 3
-name: "cat"
-unique_id: "5a38c820ed"
-productgroup: PersistentCollection {#7464 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7460}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7465 ▼
-elements: array:1 [▼
0 => Productgroup {#7146 ▼
-id: 6
-name: "Animals"
-unique_id: "9e4ef1c46f"
-fields: PersistentCollection {#7357 ▶}
}
]
}
#initialized: true
}
-type: Type {#7541 ▶}
}
1 => Fields {#7542 ▼
-id: 4
-name: "horse"
-unique_id: "bd7762b0e6"
-productgroup: PersistentCollection {#7543 ▼
-snapshot: array:1 [ …1]
-owner: Fields {#7542}
-association: array:20 [ …20]
-em: EntityManager {#2815 …11}
-backRefFieldName: "fields"
-typeClass: ClassMetadata {#6494 …}
-isDirty: false
#collection: ArrayCollection {#7544 ▼
-elements: array:1 [▼
0 => Productgroup {#7146}
]
}
#initialized: true
}
-type: Type {#7545 ▶}
}
]
我的字段实体:
/**
* @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields")
* @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
*/
private $productgroup;
public function getProductgroup()
{
return $this->productgroup;
}
public function setProductgroup($productgroup): self
{
$this->productgroup = $productgroup;
return $this;
}
public function __construct()
{
$this->productgroup = new ArrayCollection();
}
首先,我认为您将 PHP 对象与 PHP 数组混淆了。但是从您的屏幕截图来看,您似乎已经得到了您想要的东西,"horse" 对象的 productgroup
关联已完全加载。
这可以在您的转储中观察到:
正如所料,猫和马对象的相关 Productgroup 对象的 uid 都是#7146,因为它们与同一产品组相关。
我认为您只是被 horse 的 Productgroup 在您的转储中不可扩展这一事实误导了。