douyue8364 2016-09-19 18:55
浏览 74
已采纳

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

I'm trying to make an easy listAction to retrieve data from Doctrine but I got this error:

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

This come out when i call the route ex.com/list that must to list the data coming from product table

Controller: (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
        );
    }
  }
}

Entity: (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;
   }
}

I can't understand what I'm missing to use. thanks.

  • 写回答

2条回答 默认 最新

  • dousheng3364 2016-09-23 20:31
    关注

    I might be wrong, but i think that is becouse You dont render anything. Try this code: $product = $this->getDoctrine() ->getRepository('AppBundle\Entity\Product') ->findAll(); return $this->render('default/index.html.twig',['product' => $product]);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备