在我的 Symfony 專案中,我將回傳所有使用Paginator定義的 Doctrine 查詢構建器的物件。
轉儲時$posts['data'],影像上會出現回應:IMAGE
當進入回圈并轉儲第一個結果時,這就是我得到的:IMAGE
我想在每個陣列物件上分配新的key-value對。每個陣列物件都有destinationId(您可以在影像上看到),并且我有一個方法可以通過該引數搜索名稱。我想將該新值分配給 foreach 中的每個物件。
代碼:
$posts = $this->entityManager->getRepository(Post::class)->getPage();
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$postsData['destinationName'] = $destinationName;
}
}
錯誤是:
Call to a member function getDestinationId() on string
這很奇怪,因為該欄位的物體型別定義為字串,并且在轉儲時也是如此
dump($post->getDestinationId());
我得到:“107869901061558”,這是字串。
uj5u.com熱心網友回復:
這是因為你覆寫了你的$postsData變數。你需要使用不同的變數來存盤你$destinationName這樣的:
$posts = $this->entityManager->getRepository(Post::class)->getPage();
$destinationNames = [];
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$destinationNames[$post->getId()] = $destinationName;
}
}
像這樣,您可以發送$destinationNames到您的模板并$destinationName通過索引找到正確的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/425515.html
上一篇:來自非默認連接的原始SQL查詢
