繁体   English   中英

Symfony 3:原则列表操作:您是否忘记了另一个名称空间的“使用”语句?

[英]Symfony 3: Doctrine listAction: Did you forget a “use” statement for another namespace?

我试图做一个简单的listAction从学说中检索数据,但出现此错误:

Attempted to load class "ProductRepository" from namespace "AppBundle\Entity".
Did you forget a "use" statement for another namespace?

当我致电必须列出来自产品表的数据的路由ex.com/list时,就会出现此问题

控制器: (src / AppBundle / Controller)

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use AppBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;


new Translator('it', new MessageSelector(), __DIR__.'/../cache');

class DefaultController extends Controller
{
/**
 * @Route("/", name="homepage")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function indexAction(Request $request)
{
    $request->getLocale();

    $request->setLocale('it');

    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
    ]);
}

/**
 *
 * @Route("list", name="list")
 * @return Response
 *
 */
public function listAction()
{
    return $product = $this->getDoctrine()
        ->getRepository('AppBundle\Entity\Product')
        ->findAll();

   /* if (!$product) {
        throw $this->createNotFoundException(
            'Nessun prodotto trovato'
        );
    }*/

}

/**
 * @Route("create",name="create")
 * @return Response
 */
public function createAction()
{
    $product = new Product();
    $product->setName('Pippo Pluto');
    $product->setPrice('19.99');
    $product->setDescription('Lorem ipsum dolor');
    $product->setSeller('1');

    $em = $this->getDoctrine()->getManager();

    $em->persist($product);
    $em->flush();

    return $this->render('default/index.html.twig');
}

/**
 * @param $id
 * @Route("show/{id}",name="show/{id}")
 * @return Response
 */
public function showAction($id)
{
    $product = new Product();
    $product = $this->getDoctrine()
        ->getRepository('AppBundle:Product')
        ->find($id);

    if (!$product) {
        throw $this->createNotFoundException(
            'Nessun prodotto trovato per l\'id '.$id
        );
    }
  }
}

实体: (src / AppBundle / Entity)

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository")
*/
class Product
{
/**
 * @var int
 *
 * @ORM\Column(name="`id`", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="`name`", type="string")
 */
private $name;
/**
 * @var float
 *
 * @ORM\Column(name="`price`", type="float")
 */
private $price;
/**
 * @var string
 *
 * @ORM\Column(name="`description`", type="string")
 */
private $description;
/**
 * @var int
 *
 * @ORM\Column(name="`seller`", type="integer")
 */
private $seller;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 *
 * @return Product
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}
/**
 * Set price
 *
 * @param float $price
 *
 * @return Product
 */
public function setPrice($price) {
    $this->price = $price;

    return $this;
}
/**
 * Set description
 *
 * @param string $description
 *
 * @return Product
 */
public function setDescription($description) {
    $this->description = $description;

    return $this;
}
/**
 * Set seller
 *
 * @param int $seller
 *
 * @return Product
 */
public function setSeller($seller) {
    $this->seller = $seller;

    return $this;
}
/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}
/**
 * Get price
 *
 * @return float
 */
public function getPrice()
{
    return $this->price;
}
/**
 * Get description
 *
 * @return string
 */
public function getDescription()
{
    return $this->description;
}
/**
 * Get seller
 *
 * @return int
 */
public function getSeller()
   {
    return $this->seller;
   }
}

我不明白我缺少使用什么。 谢谢。

发生该错误“尝试从名称空间“ AppBundle \\ Entity”加载类“ ProductRepository”,因为我猜错了,您在AppBundle/Entity/Product类中重命名了repositoryClass

因此,您应该拥有* @ORM\\Entity(repositoryClass="AppBundle\\Repository\\ProductRepository")而不是* @ORM\\Entity(repositoryClass="AppBundle\\Entity\\ProductRepository") * @ORM\\Entity(repositoryClass="AppBundle\\Repository\\ProductRepository") ,这意味着应该定位存储库类进入Repository目录而不是Entity

控制器操作方法必须始终返回响应或重定向等。

而且,即使您的方法也findAll() ,您始终希望在存储库对象而不是实体对象上调用findAll()方法。 因此,再次,在您的listAction而不是->getRepository('AppBundle\\Entity\\Product')->findAll()需要是->getRepository('AppBundle:Product')->findAll()

我可能是错的,但是我认为这是因为您不渲染任何东西。 尝试以下代码: $product = $this->getDoctrine() ->getRepository('AppBundle\\Entity\\Product') ->findAll(); return $this->render('default/index.html.twig',['product' => $product]); $product = $this->getDoctrine() ->getRepository('AppBundle\\Entity\\Product') ->findAll(); return $this->render('default/index.html.twig',['product' => $product]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM