Symfony 3:Doctrine listAction:您是否忘记了另一个命名空间的“use”语句?

时间:2016-09-19 18:55:38

标签: php symfony

我正在尝试创建一个简单的listAction来从Doctrine中检索数据,但是我收到了这个错误:

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;
   }
}

我无法理解我错过了什么。感谢。

2 个答案:

答案 0 :(得分:1)

该错误&#34;尝试加载类&#34; ProductRepository&#34;来自命名空间&#34; AppBundle \ Entity&#34;之所以生成,是因为我错误地将您重命名为repositoryClass课程中的AppBundle/Entity/Product

因此,* @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository")代替* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository"),而不是Repository,这意味着存储库类应位于Entity目录而不是findAll()

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

而且,即使您的方式也有效,您总是希望在存储库对象上调用listAction方法,而不是实体1。所以,再次, 在->getRepository('AppBundle\Entity\Product')->findAll()而不是->getRepository('AppBundle:Product')->findAll()中需要VirtualHost

答案 1 :(得分:0)

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