我怎样才能让我的阵列(Symfony的4)内访问ArrayCollection的数据?

问题描述 投票:0回答:1

fields table

enter image description here

productgroup table

enter image description here

在我的控制器我打开我的fields_array是这样的:

$fields_array = $this->getDoctrine()->getRepository(class::fields)->findAll();

如果字段和productgroup没有连接我的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不包含任何元素。

所以,现在我与productgroup cat连接Animals。所以,我的表fields_productgroup看起来是这样的:

enter image description here

现在你看,我的ArrayCollectioncat包含元素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与productgroup Animals。所以,我的表fields_productgroup看起来是这样的:

enter image description here

我对fields_array显示horse,有在ArrayCollection的元素,但它不包含Animals的信息。这仅仅是一个空数组...但我真的需要它productgroup马连接信息

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();
  }
arrays symfony doctrine arraycollection
1个回答
1
投票

首先,我想你混淆了与PHP数组的PHP对象。但是从你的截图来看,似乎你有你想要什么,“马”对象都有其productgroup协会满载。

这可以在你的转储可以观察到:

Cat dump -- Related Productgroup uid is #7146 Horse dump -- Related Productgroup uid is also #7146

正如人们所看到的相关Productgroup对象的uid是#7146的猫和马的对象都如预期,因为它们都涉及相同的产品群。

我认为,你刚才已经被事实马的Productgroup是不是在你的转储扩张的误导。

© www.soinside.com 2019 - 2024. All rights reserved.