Php Symfony2.8-缺少标识符id

Php Symfony2.8-缺少标识符id,php,symfony-2.8,Php,Symfony 2.8,我正在开发一个基于symfony2.8的网站,允许用户购买产品。我在处理购物车时不断遇到问题。以下是返回的错误: MainBundle\Entity\command查询缺少标识符id MainBundle\Entity\command: <?php namespace MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Commande * * @ORM\Table(name="commande") * @ORM

我正在开发一个基于symfony2.8的网站,允许用户购买产品。我在处理购物车时不断遇到问题。以下是返回的错误:

MainBundle\Entity\command查询缺少标识符id

MainBundle\Entity\command:

<?php

namespace MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Commande
 *
 * @ORM\Table(name="commande")
 * @ORM\Entity(repositoryClass="MainBundle\Repository\CommandeRepository")
 */
class Commande
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var bool
     *
     * @ORM\Column(name="valider", type="boolean")
     */
    private $valider;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    /**
     * @var string
     *
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="commandes")
     */
    private $user;

    /**
     * @var int
     *
     * @ORM\Column(name="reference", type="integer", nullable=true)
     */
    private $reference;

    /**
     * @var array
     *
     * @ORM\Column(name="commande", type="array", nullable=true)
     */
    private $commande;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set valider
     *
     * @param boolean $valider
     *
     * @return Commande
     */
    public function setValider($valider)
    {
        $this->valider = $valider;

        return $this;
    }

    /**
     * Get valider
     *
     * @return bool
     */
    public function getValider()
    {
        return $this->valider;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return Commande
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set reference
     *
     * @param integer $reference
     *
     * @return Commande
     */
    public function setReference($reference)
    {
        $this->reference = $reference;

        return $this;
    }

    /**
     * Get reference
     *
     * @return int
     */
    public function getReference()
    {
        return $this->reference;
    }

    /**
     * Set products
     *
     * @param array $products
     *
     * @return Commande
     */
    public function setProducts($products)
    {
        $this->products = $products;

        return $this;
    }

    /**
     * Get products
     *
     * @return array
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * Set user
     *
     * @param \UserBundle\Entity\User $user
     *
     * @return Commande
     */
    public function setUser(\UserBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set commande
     *
     * @param array $commande
     *
     * @return Commande
     */
    public function setCommande($commande)
    {
        $this->commande = $commande;

        return $this;
    }

    /**
     * Get commande
     *
     * @return array
     */
    public function getCommande()
    {
        return $this->commande;
    }
}
有什么建议吗?

方法1将
id
作为参数,因此以下代码看起来是错误的:

$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
您必须检查
$session->get('commande')
是否返回一个整数,以及它是否对应于
文章的
id
之一

如果您的代码,
command
是一个实体,同时也是该实体的一个属性,即数组。您可能应该重命名对象和变量以避免混淆


这是通往

的快捷方式。我发现,在CartController::prepareCommandeAction()中,我没有将
$em->flush()
放在正确的位置。这就是为什么prepareCommandeAction()总是返回null,因为$commande->getId()等于null。以下是我应该做的:

public function prepareCommandeAction()
    {

        $session = $this->getRequest()->getSession();
        $em = $this->getDoctrine()->getManager();

        if (!$session->has('commande')) {
            $commande = new Commande();

            dump($commande);die();

            $commande->setDate(new \DateTime());


            $commande->setUser($this->container->get('security.context')->getToken()->getUser());
            $commande->setValider(0);
            $commande->setReference(0);
            $commande->setCommande($this->facture());

            $em->persist($commande);
            $em->flush();
            $session->set('commande', $commande->getId());
        } else {
            $commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
        }





        return $commande->getId();
    }

错误到底发生在哪一行?“Commande”实体上的
->find()
方法在多个位置执行谢谢@markvdlaan93您关于我的问题的帖子。没有任何关于线路的指示,您需要提供更多信息以便正确调试。你能给出堆栈跟踪吗?@markvdlaan93我刚刚添加了堆栈跟踪信息。谢谢你愿意帮助我。
in vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php at line 294   -
     */
    public static function missingIdentifierField($className, $fieldName)
    {
        return new self("The identifier $fieldName is missing for a query of " . $className);
    }
    /**
at ORMException ::missingIdentifierField ('MainBundle\Entity\Commande', 'id') 
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 403   + 
at EntityManager ->find ('MainBundle\Entity\Commande', null, null, null) 
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php at line 154   + 
at EntityRepository ->find (null) 
in src/MainBundle/Controller/CartController.php at line 182   + 
at CartController ->prepareCommandeAction () 
in src/MainBundle/Controller/CartController.php at line 206   + 
at CartController ->validationAction () 
at call_user_func_array (array(object(CartController), 'validationAction'), array()) 
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 135   + 
at HttpKernel ->handleRaw (object(Request), '1') 
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php at line 57   + 
at HttpKernel ->handle (object(Request), '1', true) 
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php at line 67   + 
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php at line 183   + 
at Kernel ->handle (object(Request)) 
in web/app_dev.php at line 30   + 
Logs   - 1 error
INFO - The "security.context" service is deprecated since Symfony 2.6 and will be removed in 3.0. 
INFO - The Symfony\Component\Security\Core\SecurityContext class is deprecated since Symfony 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead. 
INFO - Passing a string as the $source argument of Twig_Environment::tokenize() is deprecated since version 1.27. Pass a Twig_Source instance instead. 
INFO - The Twig_Filter_Method class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead. 
INFO - The Twig_Filter class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_month" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "txt_day" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "get_schedule" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "phone_number" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "pdfcase" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - Using an instance of "Twig_Filter_Method" for filter "substr" is deprecated since version 1.21. Use Twig_SimpleFilter instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "_method" requirement of route "fos_user_security_check" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "_method" requirement of route "fos_user_resetting_send_email" in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "methods" option instead.
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing/fos.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - The "pattern" option in file "/Applications/MAMP/htdocs/bambou-afrique/src/UserBundle/Resources/config/routing.yml" is deprecated since Symfony 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead. 
INFO - Matched route "validation". 
INFO - The "form.csrf_provider" service is deprecated since Symfony 2.4 and will be removed in 3.0. Use the "security.csrf.token_manager" service instead. 
INFO - The Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter class is deprecated since Symfony 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead. 
DEBUG - Read existing security token from the session. 
DEBUG - SELECT t0.username AS username_1, t0.username_canonical AS username_canonical_2, t0.email AS email_3, t0.email_canonical AS email_canonical_4, t0.enabled AS enabled_5, t0.salt AS salt_6, t0.password AS password_7, t0.last_login AS last_login_8, t0.confirmation_token AS confirmation_token_9, t0.password_requested_at AS password_requested_at_10, t0.roles AS roles_11, t0.id AS id_12, t0.maidenname AS maidenname_13, t0.gender AS gender_14, t0.firstname AS firstname_15, t0.lastname AS lastname_16, t0.birthday AS birthday_17, t0.birthcity AS birthcity_18, t0.birthzipcode AS birthzipcode_19, t0.birthcountry AS birthcountry_20, t0.nationality AS nationality_21, t0.phone AS phone_22, t0.passwordUncrypted AS passwordUncrypted_23, t0.debug AS debug_24 FROM user t0 WHERE t0.id = ? LIMIT 1 
DEBUG - User was reloaded from a user provider. 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". 
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead. 
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead. 
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method. 
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method. 
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method. 
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method. 
INFO - The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead. 
INFO - The Symfony\Bundle\FrameworkBundle\Controller\Controller::getRequest method is deprecated since Symfony 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method. 
CRITICAL - Uncaught PHP Exception Doctrine\ORM\ORMException: "The identifier id is missing for a query of MainBundle\Entity\Commande" at /Applications/MAMP/htdocs/bambou-afrique/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php line 294 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundle\Translation\TranslationListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "MainBundle\Services\RedirectionListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". 
$commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
public function prepareCommandeAction()
    {

        $session = $this->getRequest()->getSession();
        $em = $this->getDoctrine()->getManager();

        if (!$session->has('commande')) {
            $commande = new Commande();

            dump($commande);die();

            $commande->setDate(new \DateTime());


            $commande->setUser($this->container->get('security.context')->getToken()->getUser());
            $commande->setValider(0);
            $commande->setReference(0);
            $commande->setCommande($this->facture());

            $em->persist($commande);
            $em->flush();
            $session->set('commande', $commande->getId());
        } else {
            $commande = $em->getRepository('MainBundle:Commande')->find($session->get('commande'));
        }





        return $commande->getId();
    }