4

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;
    }
}

2 Answers 2

7

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));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I thought about it but in the return..... render to type destination of twig template then array with these $id, $name like you did
No problem, let me know if that doesn't work for you
4

Just to inform other people about this: I got the same error message when I was trying to call the method find() with a null-value variable.

I was doing: find($userId) without realizing that $userId == null, and I got the error message that got me to this topic.

Hope it saves some time from someone else

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.