我有以下物體:
class Product
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\StockEntry", mappedBy="product")
*
* @Groups({"product:read", "product:read:all"})
*/
private Collection $stockEntries;
public function getStockEntries(): Collection
{
return $this->stockEntries;
}
public function getLastBuyPrice(): float
{
/** @var StockEntry $lastEntry */
$lastEntry = $this->getStockEntries()->last();
if ($lastEntry) {
return $lastEntry->getBuyPrice() ?: 0.;
}
return 0.;
}
}
我的問題是,當我呼叫我的getLastBuyPrice()方法時,所有StockEntries都被檢索,這可能很長(一個產品可以有數百個庫存條目)。我正在尋找一種重寫方法,getLastBuyPrice()以便只StockEntry檢索最新的來計算 lastBuyPrice。
uj5u.com熱心網友回復:
我建議您在“StockEntryRepository”中創建一個方法。此方法將檢索您需要的最后一個專案,而無需遍歷所有專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/358345.html
上一篇:Symfony\Bridge\Twig\Extension\RoutingExtension::getPath()中的引數錯誤
