Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4, find() an entity

Thanks to the symfony doc https://symfony.com/doc/current/doctrine.html#updating-an-object, i am able to use get/set methods from my entity called 'Produit'.

But when i call the method setProduit() (from Paniers entity) Phpstorm say "expected App\Entity\Paniers, got Object" for the $produitSelected.

i don't know why phpstorm say that because i'm able to use methods, what's the problem ? find() return an entity object, right ?

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;
  }
}
like image 666
Ruskof Avatar asked Oct 27 '25 08:10

Ruskof


2 Answers

In addition to Denis V's correct answer, I want to add that you could also modify your controller like this:

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

This way phpstorm also knows the type of the returned object, as every returned object is type hinted in the corresponding repository.

like image 154
Sebastian Avatar answered Oct 29 '25 23:10

Sebastian


PhpStorm is apparently not that smart to understand that the actual type of the return value of find($id) in this case will be Produit. But you can help it:

/** @var Produit $produitSelected */
$produitSelected = $doctrine->getRepository(Produit::class)->find($id);

To make it working you should either use Produit with the full namespace or add the namespace directly within the type hint.

Of course this doesn't guarantee or ensure that the actual type will be Produit. So, if you make a mistake, PhpStorm will report the type incorrectly.

like image 38
Denis V Avatar answered Oct 29 '25 21:10

Denis V