在实体存储库上调用方法时出现“警告:潜在的多态调用”

Lai*_*ire 4 php symfony doctrine-orm phpstorm

我的实体Item有一个ItemRepository具有该功能的存储库() findItemCount()。当我使用

\n
$repository = $em->getRepository(Item::class);    \n$items = $repository->findItemCount();\n
Run Code Online (Sandbox Code Playgroud)\n

我收到警告:

\n
\n

潜在的多态调用。根据作为参数传递的实际类实例,代码可能无法运行。

\n
\n

此外,自动完成功能也不建议我使用“findItemCount”功能。我的错误是什么?

\n

控制器:

\n
/**\n * Artikel Liste\n * @Route("/item/list", name="app_item_list")\n * @param EntityManagerInterface $em\n * @return Response\n */\npublic function listItems(EntityManagerInterface $em): Response\n{\n    $repository = $em->getRepository(Item::class);\n    $items = $repository->findItemCount();\n\n    return $this->render('item_admin/itemList.html.twig', [\n        'items' => $items,\n        'title' => 'Artikel \xc3\x9cbersicht',\n        'blocked' => false\n    ]);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

项目.php

\n
<?php\n\nnamespace App\\Entity;\n\nuse Doctrine\\Common\\Collections\\ArrayCollection;\nuse Doctrine\\Common\\Collections\\Collection;\nuse Doctrine\\ORM\\Mapping as ORM;\nuse Doctrine\\ORM\\Mapping\\OrderBy;\n\n/**\n * @ORM\\Entity(repositoryClass="App\\Repository\\ItemRepository")\n */\nclass Item\n{\n    /**\n     * @ORM\\Id()\n     * @ORM\\GeneratedValue()\n     * @ORM\\Column(type="integer")\n     */\n    private $id;\n\n    /**\n     * @ORM\\Column(type="string", length=255)\n     * @OrderBy({"name" = "ASC"})\n     */\n    private $name;\n\n    /**\n     * @ORM\\Column(type="text", nullable=true)\n     */\n    private $description;\n\n    /**\n     * @ORM\\ManyToOne(targetEntity="App\\Entity\\Itemparent", inversedBy="item")\n     */\n    private $itemparent;\n\n    /**\n     * @ORM\\ManyToOne(targetEntity="App\\Entity\\Itemgroup", inversedBy="items")\n     */\n    private $itemgroup;\n\n    /**\n     * @ORM\\Column(type="string", length=255)\n     */\n    private $minsize;\n\n    /**\n     * @ORM\\Column(type="boolean")\n     */\n    private $charge;\n\n    /**\n     * @ORM\\Column(type="boolean")\n     */\n    private $blocked;\n\n    /**\n     * @ORM\\OneToMany(targetEntity="App\\Entity\\Barcode", mappedBy="item", orphanRemoval=true)\n     */\n    private $barcodes;\n\n    /**\n     * @ORM\\OneToMany(targetEntity="App\\Entity\\ItemStock", mappedBy="item", orphanRemoval=true)\n     */\n    private $itemStocks;\n\n    /**\n     * @ORM\\OneToMany(targetEntity="App\\Entity\\BookingItem", mappedBy="item", orphanRemoval=true)\n     */\n    private $bookingItems;\n\n    /**\n     * @ORM\\Column(type="float", nullable=true)\n     */\n    private $price;\n\n    /**\n     * @ORM\\OneToMany(targetEntity=SupplierItems::class, mappedBy="item")\n     */\n    private $supplierItems;\n\n    /**\n     * @ORM\\OneToMany(targetEntity=ItemStockCharge::class, mappedBy="item")\n     */\n    private $itemStockCharges;\n\n\n    public function __construct()\n    {\n        $this->barcodes = new ArrayCollection();\n        $this->itemStocks = new ArrayCollection();\n        $this->suppliers = new ArrayCollection();\n        $this->supplierItems = new ArrayCollection();\n        $this->itemStockCharges = new ArrayCollection();\n    }\n\n    public function getId(): ?int\n    {\n        return $this->id;\n    }\n\n    public function getName(): ?string\n    {\n        return $this->name;\n    }\n\n    public function setName(string $name): self\n    {\n        $this->name = $name;\n\n        return $this;\n    }\n\n    public function getDescription(): ?string\n    {\n        return $this->description;\n    }\n\n    public function setDescription(?string $description): self\n    {\n        $this->description = $description;\n\n        return $this;\n    }\n\n    public function getItemparent(): ?Itemparent\n    {\n        return $this->itemparent;\n    }\n\n    public function setItemparent(?Itemparent $itemparent): self\n    {\n        $this->itemparent = $itemparent;\n\n        return $this;\n    }\n\n    public function getItemgroup(): ?Itemgroup\n    {\n        return $this->itemgroup;\n    }\n\n    public function setItemgroup(?Itemgroup $itemgroup): self\n    {\n        $this->itemgroup = $itemgroup;\n\n        return $this;\n    }\n\n    public function getMinsize(): ?string\n    {\n        return $this->minsize;\n    }\n\n    public function setMinsize(string $minsize): self\n    {\n        $this->minsize = $minsize;\n\n        return $this;\n    }\n\n    public function getCharge(): ?bool\n    {\n        return $this->charge;\n    }\n\n    public function setCharge(bool $charge): self\n    {\n        $this->charge = $charge;\n\n        return $this;\n    }\n\n    public function getBlocked(): ?bool\n    {\n        return $this->blocked;\n    }\n\n    public function setBlocked(bool $blocked): self\n    {\n        $this->blocked = $blocked;\n\n        return $this;\n    }\n\n    /**\n     * @return Collection|Barcode[]\n     */\n    public function getBarcodes(): Collection\n    {\n        return $this->barcodes;\n    }\n\n    public function addBarcode(Barcode $barcode): self\n    {\n        if (!$this->barcodes->contains($barcode)) {\n            $this->barcodes[] = $barcode;\n            $barcode->setItem($this);\n        }\n\n        return $this;\n    }\n\n    public function removeBarcode(Barcode $barcode): self\n    {\n        if ($this->barcodes->contains($barcode)) {\n            $this->barcodes->removeElement($barcode);\n            // set the owning side to null (unless already changed)\n            if ($barcode->getItem() === $this) {\n                $barcode->setItem(null);\n            }\n        }\n\n        return $this;\n    }\n\n    /**\n     * @return Collection|ItemStock[]\n     */\n    public function getItemStocks(): Collection\n    {\n        return $this->itemStocks;\n    }\n\n    public function addItemStock(ItemStock $itemStock): self\n    {\n        if (!$this->itemStocks->contains($itemStock)) {\n            $this->itemStocks[] = $itemStock;\n            $itemStock->setItem($this);\n        }\n\n        return $this;\n    }\n\n    public function removeItemStock(ItemStock $itemStock): self\n    {\n        if ($this->itemStocks->contains($itemStock)) {\n            $this->itemStocks->removeElement($itemStock);\n            // set the owning side to null (unless already changed)\n            if ($itemStock->getItem() === $this) {\n                $itemStock->setItem(null);\n            }\n        }\n\n        return $this;\n    }\n\n    /**\n     * @return Collection|BookingItem[]\n     */\n    public function getBookingItems(): Collection\n    {\n        return $this->bookingItems;\n    }\n\n    public function addBookingItem(BookingItem $bookingItem): self\n    {\n        if (!$this->bookingItems->contains($bookingItem)) {\n            $this->bookingItems[] = $bookingItem;\n            $bookingItem->setItem($this);\n        }\n\n        return $this;\n    }\n\n    public function removeBookingItem(BookingItem $bookingItem): self\n    {\n        if ($this->bookingItems->contains($bookingItem)) {\n            $this->bookingItems->removeElement($bookingItem);\n            if ($bookingItem->getItem() === $this) {\n                $bookingItem->setItem(null);\n            }\n        }\n        return $this;\n    }\n\n    public function getPrice(): ?float\n    {\n        return $this->price;\n    }\n\n    public function setPrice(?float $price): self\n    {\n        $this->price = $price;\n\n        return $this;\n    }\n\n    public function getCommaPrice(): string\n    {\n        return number_format($this->price, 2, ',', '');\n    }\n\n    /**\n     * @return Collection|SupplierItems[]\n     */\n    public function getSupplierItems(): Collection\n    {\n        return $this->supplierItems;\n    }\n\n    public function addSupplierItem(SupplierItems $supplierItem): self\n    {\n        if (!$this->supplierItems->contains($supplierItem)) {\n            $this->supplierItems[] = $supplierItem;\n            $supplierItem->setItem($this);\n        }\n\n        return $this;\n    }\n\n    public function removeSupplierItem(SupplierItems $supplierItem): self\n    {\n        if ($this->supplierItems->contains($supplierItem)) {\n            $this->supplierItems->removeElement($supplierItem);\n            // set the owning side to null (unless already changed)\n            if ($supplierItem->getItem() === $this) {\n                $supplierItem->setItem(null);\n            }\n        }\n\n        return $this;\n    }\n\n    /**\n     * @return Collection|ItemStockCharge[]\n     */\n    public function getItemStockCharges(): Collection\n    {\n        return $this->itemStockCharges;\n    }\n\n    public function addItemStockCharge(ItemStockCharge $itemStockCharge): self\n    {\n        if (!$this->itemStockCharges->contains($itemStockCharge)) {\n            $this->itemStockCharges[] = $itemStockCharge;\n            $itemStockCharge->setItem($this);\n        }\n\n        return $this;\n    }\n\n    public function removeItemStockCharge(ItemStockCharge $itemStockCharge): self\n    {\n        if ($this->itemStockCharges->contains($itemStockCharge)) {\n            $this->itemStockCharges->removeElement($itemStockCharge);\n            // set the owning side to null (unless already changed)\n            if ($itemStockCharge->getItem() === $this) {\n                $itemStockCharge->setItem(null);\n            }\n        }\n\n        return $this;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

项目库.php

\n
<?php\n\nnamespace App\\Repository;\n\nuse App\\Entity\\Item;\nuse Doctrine\\Bundle\\DoctrineBundle\\Repository\\ServiceEntityRepository;\nuse Doctrine\\Common\\Persistence\\ManagerRegistry;\n\n/**\n * @method Item|null find($id, $lockMode = null, $lockVersion = null)\n * @method Item|null findOneBy(array $criteria, array $orderBy = null)\n * @method Item[]    findAll()\n * @method Item[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)\n */\nclass ItemRepository extends ServiceEntityRepository\n{\n    public function __construct(ManagerRegistry $registry)\n    {\n        parent::__construct($registry, Item::class);\n    }\n\n    public function findItem()\n    {\n        return $this->createQueryBuilder('a')\n            ->andWhere('a.blocked = :val')\n            ->setParameter('val', false)\n            ->orderBy('a.name', 'ASC')\n            ->getQuery()\n            ->getResult()\n            ;\n    }\n\n    public function findBlockedItemCount()\n    {\n        return $this->createQueryBuilder('item')\n            ->select('item, SUM(stocks.count) as sums')\n            ->andWhere('item.blocked = :val')\n            ->setParameter('val', true)\n            ->leftJoin('item.itemStocks', 'stocks')\n            ->groupBy('item')\n            ->orderBy('item.name', 'ASC')\n            ->getQuery()\n            ->getResult()\n            ;\n    }\n\n    public function findItemCount(){\n        return $this->createQueryBuilder('item')\n            ->select('item, SUM(stocks.count) as sums')\n            ->andWhere('item.blocked = :val')\n            ->setParameter('val', false)\n            ->leftJoin('item.itemStocks', 'stocks')\n            ->groupBy('item')\n            ->orderBy('item.name', 'ASC')\n            ->getQuery()\n            ->getResult()\n            ;\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

yiv*_*ivi 7

IDE 无法知道$em->getRepository(Item::class);将返回ItemRepository,因为返回对象的类型直到运行时才解析。

注入ItemRepository而不是实体管理器,在任何情况下都是更好的做法:

public function listItems(ItemRepository $itemRepository): Response
{
    $items = $itemRepository->findItemCount();
    // etc
}
Run Code Online (Sandbox Code Playgroud)