@ParamConverter注释未找到Symfony 4.2 App\Entity\Shopcart对象

@ParamConverter注释未找到Symfony 4.2 App\Entity\Shopcart对象,symfony,symfony4,Symfony,Symfony4,我在做symfony4.2。我得到这个错误 App\Entity\Shopcart object not found by the @ParamConverter annotation. 当我尝试删除或编辑shopcard上的项目时。我尝试了许多解决方案,但都失败了。我需要您的建议 这是我的购物车管理员 <?php namespace App\Controller; use App\Entity\Shopcart; use App\Form\ShopcartType; use App

我在做symfony4.2。我得到这个错误

App\Entity\Shopcart object not found by the @ParamConverter annotation.
当我尝试删除或编辑shopcard上的项目时。我尝试了许多解决方案,但都失败了。我需要您的建议

这是我的购物车管理员

<?php

namespace App\Controller;

use App\Entity\Shopcart;
use App\Form\ShopcartType;
use App\Repository\ProductRepository;
use App\Repository\ShopcartRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/shopcart")
 */
class ShopcartController extends AbstractController
{
    /**
     * @Route("/", name="shopcart_index", methods={"GET"})
     */
    public function index(ShopcartRepository $shopcartRepository, ProductRepository $productRepository): Response
    {
        $slider=$productRepository->findBy([], ['name'=>'ASC'], 3);     //slider doesn't exist hatası için ekledim

        return $this->render('shopcart/index.html.twig', [
            'shopcarts' => $shopcartRepository->getAllShops(),
            'slider' => $slider,    //slider doesn't exist hatası için ekledim

        ]);
    }

    /**
     * @Route("/new", name="shopcart_new", methods={"GET","POST"})
     */
    public function new(Request $request): Response
    {
        $shopcart = new Shopcart();
        $form = $this->createForm(ShopcartType::class, $shopcart);
        $form->handleRequest($request);
        echo $submittedToken=$request->request->get('token');
        if($this->isCsrfTokenValid('add-item', $submittedToken)) {      //alınan tokenla uyuşuyorsa göre sepete ekliyor
            if ($form->isSubmitted()) {
                $entityManager = $this->getDoctrine()->getManager();
                $user=$this->getUser();

                $shopcart->setQuantity($request->request->get('quantity'));
                $shopcart->setUserid($user->getid());

                $entityManager->persist($shopcart);
                $entityManager->flush();

                return $this->redirectToRoute('shopcart_index');
            }
        }
        return $this->render('shopcart/new.html.twig', [
            'shopcart' => $shopcart,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="shopcart_show", methods={"GET"})
     */
    public function show(Shopcart $shopcart): Response
    {
        return $this->render('shopcart/show.html.twig', [
            'shopcart' => $shopcart,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="shopcart_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Shopcart $shopcart): Response
    {
        $form = $this->createForm(ShopcartType::class, $shopcart);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('shopcart_edit',['id'=>$shopcart->getId()]);
        }

        return $this->render('shopcart/edit.html.twig', [
            'shopcart' => $shopcart,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="shopcart_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Shopcart $shopcart): Response
    {

        //methodu post ile değiştirip dene
        if ($this->isCsrfTokenValid('delete'.$shopcart->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($shopcart);
            $entityManager->flush();
        }

        return $this->redirectToRoute('shopcart_index');
    }


    /**
     * @Route("/{id}/del", name="shopcart_del", methods={"DELETE"})
     */
    public function del(Request $request, Shopcart $shopcart): Response
    {
        $em=$this->getDoctrine()->getManager();
        $em->remove($shopcart);
        $em->flush();

        return $this->redirectToRoute('shopcart_index');
    }
}

我认为问题在于您有两个删除函数。del函数是delete函数的副本。如果删除“del”函数,您的问题将得到解决。“del”函数会覆盖路由和其他函数。

如果您这样做,它会报告相同的错误吗?是的,我尝试了,但我得到了相同的错误。此外,控制器上的“new”方法没有问题。这是意料之中的,因为
new
不使用参数转换器。我假设您使用
make:entity
命令构建了
Shopcart
,该命令自动创建了回购并设置了
id
属性?您向删除和编辑路径传递了什么参数?你能在浏览器中复制网址吗?有什么帮助吗?问题解决了。错误源是错误的sql查询。
<?php

namespace App\Repository;

use App\Entity\Shopcart;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use phpDocumentor\Reflection\Types\Integer;

/**
 * @method Shopcart|null find($id, $lockMode = null, $lockVersion = null)
 * @method Shopcart|null findOneBy(array $criteria, array $orderBy = null)
 * @method Shopcart[]    findAll()
 * @method Shopcart[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ShopcartRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Shopcart::class);
    }

    // /**
    //  * @return Shopcart[] Returns an array of Shopcart objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('s')
            ->andWhere('s.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('s.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Shopcart
    {
        return $this->createQueryBuilder('s')
            ->andWhere('s.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */

    //LET JOIN WITH SQL
    public function getAllShops():array
    {
        $conn = $this->getEntityManager()->getConnection();
        $sql = '
    SELECT p.name, p.price, s.*, u.id FROM shopcart s, product p, user u
    WHERE s.productid = p.id and s.userid=u.id
     ';
        $stmt=$conn->prepare($sql);
        $stmt->execute();

        return $stmt->fetchAll();
    }


    public function getUserShopCartTotal($userid): float
    {
        $em=$this->getEntityManager();
        $query= $em->createQuery('
        SELECT sum(p.price *s .quantity) as total
            FROM App\Entity\Shopcart s, App\Entity\Product p
            WHERE  s.productid = p.id and s.userid=:userid
')
            ->setParameter('userid',$userid);
        $result=$query->getResult();

        if($result[0]['total']!=null){
            return $result[0]["total"];
        } else {
            return 0;
        }
    }

    public function getUserShopCartCount($userid): Integer
    {
        $em=$this->getEntityManager();
        $query= $em->createQuery('
        SELECT count(s.id) as shopcount
            FROM App\Entity\Shopcart s
            WHERE s.userid=:userid
')
            ->setParameter('userid',$userid);
        $result=$query->getResult();

        if($result[0]['shopcount']!=null){
            return $result[0]["shopcount"];
        } else {
            return 0;
        }
    }

    public function getUserShopCart($userid): array
    {
        $em=$this->getEntityManager();
        $query= $em->createQuery('
        SELECT p.name, p.price, s.quantity,s.productid, s.userid, (p.price * s.quantity) as total
            FROM App\Entity\Shopcart s, App\Entity\Product p
            WHERE s.productid = p.id and s.userid=:userid
')
            ->setParameter('userid',$userid);
        return $query->getResult();
    }

}