I want to get from the database $id
and $name
but I get this exception:
The identifier id is missing for a query of Grupa\ProjektBundle\Entity\Car
I have GeneratedValue(strategy="AUTO")
in Doctrine annotations in entity Car
.
How can I fix this? The routing matches!
Additionally I have a database entry with an id of 1 and name with the value of some URL that points to an image (http://www.supercarworld.com/images/fullpics/595.jpg).
This is my Controller called SupercarsController.php:
namespace Grupa\ProjektBundle\Controller;
use Grupa\ProjektBundle\Entity\Car;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class SupercarsController extends Controller
{
public function scAction($id = null, $name = null){
$car = new Car();
// $car->setName('http://www.supercarworld.com/images/fullpics/595.jpg');
// em entity manager
// $em = $this->getDoctrine()->getManager();
// $em->persist($car);
// $em->flush();
$car = $this->getDoctrine()
->getRepository('GrupaProjektBundle:Car')
->find($id, $name);
return $this->render('GrupaProjektBundle:Sc:supercars.html.twig');
}
public function showAction($slug)
{
$url = $this->generateUrl('grupa_projekt_supercars',
array('slug' => 'marka')
);
return $this->render('GrupaProjektBundle:Sc:makes.html.twig');
}
}
This is my entity Car.php:
namespace Grupa\ProjektBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Car
{ /**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
* @ORM\Column(type="string")
*
*/
protected $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $id;
}
/**
* Set name
*
* @param string $name
*
* @return Car
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
You are calling the find()
function incorrectly. Since ID is unique, you really only have to call your method on the ID, like so:
$car = $this->getDoctrine()
->getRepository('GrupaProjektBundle:Car')
->find($id);
However, you can still search on both the id and the name if you want to, but you'll have to change your query to:
$car = $this->getDoctrine()
->getRepository('GrupaProjektBundle:Car')
->findOneBy(array('id' => $id, 'name' => $name));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With