簡體   English   中英

如何獲得對我的Array(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 所以我的表fields_productgroup看起來像這樣:

在此處輸入圖片說明

現在,如您所見,我的cat 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: []
      -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 所以我的表fields_productgroup看起來像這樣:

在此處輸入圖片說明

fields_arrayhorse表明,有在一個元素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數組混淆。 但是從您的屏幕截圖來看,您似乎確實擁有了想要的東西,“馬”對象已完全加載了其productgroup關聯。

這可以在您的轉儲中觀察到:

貓轉儲-相關產品組uid是#7146 Horse dump-相關產品組uid也是#7146

可以看到,貓和馬對象的相關Productgroup對象的uid為#7146,這與預期的一樣,因為它們與同一產品組相關。

我認為您只是被馬的Productgroup在您的轉儲中不可擴展這一事實所誤導。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM