Symfony2 returns a variable identifier with objects from a database

I want to get from the database $idand $name, but I get this exception:

No id for request Grupa \ ProjektBundle \ Entity \ Car

I have GeneratedValue(strategy="AUTO")in the annotations of Doctrine in essence Car. How can i fix this? Routing is consistent!

In addition, I have a database record with identifier 1 and a name with the value of some URL pointing to the 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 essence 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;
    }
}
+4
2

find(). , , :

$car = $this->getDoctrine()
    ->getRepository('GrupaProjektBundle:Car')
    ->find($id);

, , , :

$car = $this->getDoctrine()
    ->getRepository('GrupaProjektBundle:Car')
    ->findOneBy(array('id' => $id, 'name' => $name));
+7

: , find() .

: find($userId), , $userId == null, , .

, - .

+4

Source: https://habr.com/ru/post/1628381/


All Articles