2017-09-05 15 views
0

我需要得到约为了所有信息为我的电子商务,但我得到这个错误 需要你们的帮助 我使用会话symfony的会话才能绑定实体查询参数只允许有一个标识符实体

的消息不长

查询参数的绑定实体只允许具有标识符的实体。 ORMInvalidArgumentException

感谢您的帮助

我会后我的实体文件,只是在几秒钟内 只是吼叫

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

屏幕截图错误 enter image description here

+0

你能告诉其上线,你得到这个错误? –

回答

0

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

更改您这样的代码:

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

谢谢,但不起作用 – wildnano

相关问题