symfony 会话顺序将实体绑定到查询参数仅允许具有标识符的实体 [英] symfony session order Binding entities to query parameters only allowed for entities that have an identifier

查看:25
本文介绍了symfony 会话顺序将实体绑定到查询参数仅允许具有标识符的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取有关电子商务订单的所有信息,但出现此错误需要你的帮助我正在使用会话

I need to get all information about an order for my e-commerce but I get this error Need your help I'm using session

消息不长

将实体绑定到查询参数仅允许具有标识符的实体.ORMInvalidArgumentException

感谢您的帮助

我会在几秒钟内发布我的实体文件就在下面

I will post my entity file just in few seconds just below

   <?php

    namespace Gba\GbaBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
    use Gba\GbaBundle\Entity\ClientsAdresses;
    use Gba\GbaBundle\Entity\Commandes;
    use Gba\GbaBundle\Entity\Produits;


        [class CommandesController extends Controller
        {
            public function facture(Request $request)
            {
                $em = $this->getDoctrine()->getManager();
                //$random = random_bytes(10);
               // $generator = $this->container->get('security.secure_random');
                // servira à générer des chaîne aléatoires qui serviront de token
                $session = $request->getSession();
                $adresse = $session->get('adresse');
                $panier = $session->get('panier');
                $commande = array(); // on déclare un tableau commande
                $totalHT = 0;
                $totalTTC = 0;

                $facturation = $em->getRepository('GbaBundle:ClientsAdresses')->find($adresse\['facturation'\]);
                $livraison = $em->getRepository('GbaBundle:ClientsAdresses')->find($adresse\['livraison'\]);
                $produits = $em->getRepository('GbaBundle:Produits')->findArray(array_keys($session->get('panier')));

                foreach($produits as $produit)
                {
                    $prixHT = ($produit->getPrix() * $panier\[$produit->getId()\]);
                    $prixTTC = ($produit->getPrix() * $panier\[$produit->getId()\] / $produit->getTva()->getMultiplicate());
                    $totalHT += $prixHT;
                    $totalTTC += $prixTTC;

                     // tableau commande séréalisé par Doctrine
                    if (!isset($commande\['tva'\]\['%'.$produit->getTva()->getValeur()\]))
                    { $commande\['tva'\]\['%'.$produit->getTva()->getValeur()\] = round($prixTTC - $prixHT,2);}
                    else{
                    $commande\['tva'\]\['%'.$produit->getTva()->getValeur()\] += round($prixTTC - $prixHT,2);}

                    $commande\['produit'\]\[$produit->getId()\] = array('reference' => $produit->getNom(),
                                                                    'quantite' => $panier\[$produit->getId()\],
                                                                    'prixHT' => round($produit->getPrix(),2),
                                                                    'prixTTC' => round($produit->getPrix() / $produit->getTva()->getMultiplicate(),2));
                }  

                $commande\['livraison'\] = array('prenom' => $livraison->getPrenom(),
                                            'nom' => $livraison->getNom(),
                                            'telephone' => $livraison->getTelephone(),
                                            'adresse' => $livraison->getAdresse(),
                                            'cp' => $livraison->getCp(),
                                            'ville' => $livraison->getVille(),
                                            'pays' => $livraison->getPays(),
                                            'complement' => $livraison->getComplement());
                $commande\['facturation'\] = array('prenom' => $facturation->getPrenom(),
                                            'nom' => $facturation->getNom(),
                                            'telephone' => $facturation->getTelephone(),
                                            'adresse' => $facturation->getAdresse(),
                                            'cp' => $facturation->getCp(),
                                            'ville' => $facturation->getVille(),
                                            'pays' => $facturation->getPays(),
                                            'complement' => $facturation->getComplement());

                $commande\['prixHT'\] = round($totalHT, 2);
                $commande\['prixTTC'\] = round($totalTTC, 2);
               // $commande\['token'\] = bin2hex($generator->nextBytes(20));

                return $commande;
            }

              public function prepareCommandeAction(Request $request)
            {
                // on préstocke les données du panier
                $session = $request->getSession();
                $em = $this->getDoctrine()->getManager();

                if (!$session->has('commande'))
                { $commande = new Commandes();}
                else
               {$commande = $em->getRepository('GbaBundle:Commandes')->find($session->get('commande'));}

                $commande->setDateCommande(new \DateTime());
                $commande->setDateStatut(new \DateTime());
                $commande->setClient($this->container->get('security.token_storage')->getToken()->getUser());
                $commande->setStatut(0);
                $commande->setReferencecom(1);
                $commande->setStatut(0);
                $commande->setCommande($this->facture($request)); // on stocke toute la commande à partir de la méthode facture

                if (!$session->has('commande')) {
                    $em->persist($commande);
                    $session->set('commande',$commande);
                }

                $em->flush();

                return new Response($commande->getId());
            }
        }

----实体

<?php

namespace Gba\GbaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Commandes
 *
 * @ORM\Table(name="commandes")
 * @ORM\Entity(repositoryClass="Gba\GbaBundle\Repository\CommandesRepository")
 */
class Commandes
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Gba\GbaUserBundle\Entity\Clients", inversedBy="commandes")
     * @ORM\JoinColumn(nullable=true)
     */
    private $client;

    /**
     * @var string
     *
     * @ORM\Column(name="statut", type="string", length=125)
     */
    private $statut;

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="referencecom", type="string", length=125)
     */
    private $referencecom;

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


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

    /**
     * Set statut
     *
     * @param string $statut
     *
     * @return Commandes
     */
    public function setStatut($statut)
    {
        $this->statut = $statut;

        return $this;
    }

    /**
     * Get statut
     *
     * @return string
     */
    public function getStatut()
    {
        return $this->statut;
    }

    /**
     * Set dateCommande
     *
     * @param \DateTime $dateCommande
     *
     * @return Commandes
     */
    public function setDateCommande($dateCommande)
    {
        $this->dateCommande = $dateCommande;

        return $this;
    }

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

    /**
     * Set dateStatut
     *
     * @param \DateTime $dateStatut
     *
     * @return Commandes
     */
    public function setDateStatut($dateStatut)
    {
        $this->dateStatut = $dateStatut;

        return $this;
    }

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

    /**
     * Set referencecom
     *
     * @param string $referencecom
     *
     * @return Commandes
     */
    public function setReferencecom($referencecom)
    {
        $this->referencecom = $referencecom;

        return $this;
    }

    /**
     * Get referencecom
     *
     * @return string
     */
    public function getReferencecom()
    {
        return $this->referencecom;
    }

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

        return $this;
    }

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

    /**
     * Set client
     *
     * @param \Gba\GbaUserBundle\Entity\Clients $client
     *
     * @return Commandes
     */
    public function setClient(\Gba\GbaUserBundle\Entity\Clients $client = null)
    {
        $this->client = $client;

        return $this;
    }

    /**
     * Get client
     *
     * @return \Gba\GbaUserBundle\Entity\Clients
     */
    public function getClient()
    {
        return $this->client;
    }
}

截图错误在此处输入图片描述

推荐答案

在你做之前 $em->flush(); 实体并不是真正持久化的,也没有 ID.

Before you do $em->flush(); the Entity is not really persisted and has no ID.

像这样改变你的代码:

if (!$session->has('commande')) {
    $em->persist($commande);
    $em->flush();
    $session->set('commande',$commande);
}

这篇关于symfony 会话顺序将实体绑定到查询参数仅允许具有标识符的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆