php - Symfony 4,find() 一个实体

标签 php symfony controller entity symfony4

感谢 symfony 文档 https://symfony.com/doc/current/doctrine.html#updating-an-object ,我可以从名为“Produit”的实体中使用 get/set 方法。

但是当我调用 setProduit() 方法(来自 Paniers 实体)时,Phpstorm 对 $produitSelected 说“预期的 App\Entity\Paniers,得到了对象”。

我不知道为什么 phpstorm 这么说,因为我可以使用方法,有什么问题? find() 返回一个实体对象,对吧?

class PaniersController extends AbstractController
{

    /**
    * @Route("/paniers/add/{id}", name="paniers.add")
    */
    public function addPanier(Request $request, Environment $twig, RegistryInterface $doctrine, $id)
    {
        $produitSelected = $doctrine->getRepository(Produit::class)->find($id);
        if (!$produitSelected) {
            throw $this->createNotFoundException(
                'Pas de produit trouvé pour l\' id : '.$id
            );
        }
        $lignePanier=$doctrine->getRepository(Paniers::class)->findOneBy(['produit' => $produitSelected, 'user' =>$this->getUser()]);
        if($produitSelected->getStock()>0){
            if ($lignePanier){
                $quantite =$lignePanier->getQuantite();
                $lignePanier->setQuantite($quantite+1);
            } else {
                $lignePanier = new Paniers();
                $lignePanier->setUser($this->getUser())
                ->setDateAjoutPanier(new \DateTime(date('Y-m-d')))
                ->setQuantite(1)
                ->setProduit($produitSelected);
            }
            $doctrine->getManager()->persist($lignePanier);
            $produitSelected->setStock($produitSelected->getStock()-1);
            $doctrine->getManager()->persist($produitSelected);
            $doctrine->getManager()->flush();
        }
    }
}

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PaniersRepository")
 * @ORM\Table(name="paniers")
 */
class Paniers
{

//...

   /**
   * @ORM\ManyToOne(targetEntity="Produit")
   * @ORM\JoinColumn(name="produit_id", referencedColumnName="id")
   */
  private $produit;

     public function getProduit(): ?Produit
  {
      return $this->produit;
  }

  public function setProduit(?Produit $produit): self
  {
      $this->produit = $produit;
      return $this;
  }
}

最佳答案

除了 Denis V 的正确答案之外,我想补充一点,您还可以像这样修改您的 Controller :

public function addPanier(Request $request, Environment $twig, RegistryInterface $doctrine,ProduitRepository $produitRepository, $id)
        {
            $produitSelected = $produitRepostiory->find($id);
            //rest of the code
       }

这样 phpstorm 也知道返回对象的类型,因为每个返回的对象在相应的存储库中都有类型提示。

关于php - Symfony 4,find() 一个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692138/

相关文章:

mysql - Symfony,OneToMany 属性的 Doctrine 顺序

ruby-on-rails - Rails : most Quiz attributes passing to params, 除了 user_id 之外。为什么?

php - 使用 WEB Apache 服务器(或任何其他 Web 服务器)读取由已编译的 c 文件(即 .exe)生成的内容

php - TinyMCE:打包所有文件而不是动态加载它们......可能吗?

php - 检查进程是否仍在运行?

database - 使用执行的 Doctrine 查询

javascript - 如何使用提供的参数将 ajax 调用限制在范围内?

php - 与数据库连接的 parameters.yml 和 config_(dev|stage|prod).yml 混淆

grails - Grails Controller 添加实例

javascript - 使用 AngularFire 简化 Angular Controller 发布到 Firebase