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

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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度