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



我正在开发Symfony 4.2。我收到这个错误

AppEntityShopcart object not found by the @ParamConverter annotation.

当我试图删除或编辑购物卡上的商品时。我尝试了许多解决方案,但都失败了。我需要你的建议。

这是我的ShopcartController

<?php
namespace AppController;
use AppEntityShopcart;
use AppFormShopcartType;
use AppRepositoryProductRepository;
use AppRepositoryShopcartRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
/**
* @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');
}
}

这是ShopcartRepository。我只使用getAllShops方法。

<?php
namespace AppRepository;
use AppEntityShopcart;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use DoctrinePersistenceManagerRegistry;
use phpDocumentorReflectionTypesInteger;
/**
* @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 AppEntityShopcart s, AppEntityProduct 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 AppEntityShopcart 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 AppEntityShopcart s, AppEntityProduct p
WHERE s.productid = p.id and s.userid=:userid
')
->setParameter('userid',$userid);
return $query->getResult();
}
}

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

相关内容

最新更新