Как я могу получить доступ к данным ArrayCollection внутри моего массива (Symfony 4)? - PullRequest
0 голосов
/ 12 февраля 2019

fields table:

enter image description here

productgroup table:

enter image description here

В моем контроллере я загружаю свой 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 выглядит следующим образом:

enter image description here

Теперь, как вы видите, моя ArrayCollection из 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.Итак, моя таблица fields_productgroup выглядит следующим образом:

enter image description here

Мой fields_array для horse показывает, что в * 1056 есть элемент* но он не содержит информацию для 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();
  }

1 Ответ

0 голосов
/ 12 февраля 2019

Прежде всего, я думаю, что вы путаете объекты PHP с массивами PHP.Но, судя по вашим скриншотам, кажется, что вы получили именно то, что хотели, у объекта "horse" полностью загружена ассоциация productgroup.

Это можно наблюдать в ваших свалках:

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

Как можно увидеть связанныеИдентификатор uid объекта Productgroup равен # 7146 для объекта cat и horse, как и ожидалось, поскольку они относятся к одной и той же группе продуктов.

Я думаю, что вы просто были введены в заблуждение тем фактом, что группа товаров лошади не может быть расширена в вашей свалке.

...