【问题标题】:How can I get access to ArrayCollection data inside my Array (Symfony 4)?如何访问我的 Array (Symfony 4) 中的 ArrayCollection 数据?
【发布时间】:2019-02-12 09:00:26
【问题描述】:

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_array 用于horse 显示,ArrayCollection 中有一个元素,但它不包含Animals 的信息。它只是一个空数组......但我实际上需要连接到哪个productgroup horse 的信息

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();
  }

【问题讨论】:

  • 您忘记添加获取数据的方式以及映射实体关联的方式(最重要的信息),但我很确定这是重复的。
  • @gp_sflover 我将实体数据添加到我的问题中。我测试了你关于 fetch EAGER @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields", cascade={"all"}, fetch="EAGER") 的建议,但没有得到不同的结果

标签: arrays symfony doctrine arraycollection


【解决方案1】:

首先,我认为您将 PHP 对象与 PHP 数组混淆了。但是从您的屏幕截图来看,您似乎拥有您想要的东西,“马”对象的productgroup 关联已满载。

这可以在您的转储中观察到:

正如我们所见,猫和马对象的相关 Productgroup 对象的 uid 都是 #7146,正如预期的那样,因为它们与同一个产品组相关。

我认为您只是被马的 Productgroup 在您的转储中不可扩展的事实误导了。

【讨论】:

  • 所以这意味着,它确实存在?只是不可扩展?
  • 没错。我猜您并没有尝试在实践中使用它,只是将其丢弃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多