繁体   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