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.