Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 issue variable id with getting objects from database

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;
    }
}
like image 351
grzesiekmq Avatar asked Dec 18 '22 19:12

grzesiekmq


1 Answers

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));
like image 78
Jason Roman Avatar answered Feb 20 '23 01:02

Jason Roman