Doctrine entity error
Solved
link897
-
francois -
francois -
Hello,
I've had a problem for a day with an error message and I can't find any answers anywhere. I followed a tutorial to learn Symfony and I keep getting this error message:
Symfony tells me that it comes either from this line:
or from this one:
Thanks in advance
Configuration: Windows / Chrome 57.0.2987.133
I've had a problem for a day with an error message and I can't find any answers anywhere. I followed a tutorial to learn Symfony and I keep getting this error message:
Uncaught PHP Exception Doctrine\ORM\ORMInvalidArgumentException: "Binding entities to query parameters only allowed for entities that have an identifier." at C:\wamp\www\Project\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php line 188
Context: { "exception": "Exception(Doctrine\\ORM\\ORMInvalidArgumentException): Binding entities to query parameters only allowed for entities that have an identifier." }
Symfony tells me that it comes either from this line:
$commande = $em->getRepository('EcommerceBundle:Commande')->find($session->get('commande')); or from this one:
$prepareCommande = $this->forward('EcommerceBundle:Commandes:prepareCommande',array('request' => $request)); Thanks in advance
Configuration: Windows / Chrome 57.0.2987.133
3 answers
Hello,
the find() method expects an ID (the primary key value, which by default is an incremental integer) and nothing else!
So, if the value of $session->get('commande') is anything other than an ID, then it's normal for it to cause an error.
Let's assume you have an ID (incremental integer) and that each entity has a order number "$numeroCmde" and that $session->get('commande') corresponds to this order number, then you should use:
findByNumeroCmde($session->get('commande'))
or
findBy(array('numeroCmde' => $session->get('commande')));
--
No, no, we can't!
Founding member of FJH
v(^_^)v
the find() method expects an ID (the primary key value, which by default is an incremental integer) and nothing else!
So, if the value of $session->get('commande') is anything other than an ID, then it's normal for it to cause an error.
Let's assume you have an ID (incremental integer) and that each entity has a order number "$numeroCmde" and that $session->get('commande') corresponds to this order number, then you should use:
findByNumeroCmde($session->get('commande'))
or
findBy(array('numeroCmde' => $session->get('commande')));
--
No, no, we can't!
Founding member of FJH
v(^_^)v
Hi,
Thank you for your response, I followed the instructions and I no longer have the error message.
However, I am not retrieving anything in my variable $commande nor in $prepareCommande
PanierController:
CommandesController:
Thank you for your response, I followed the instructions and I no longer have the error message.
However, I am not retrieving anything in my variable $commande nor in $prepareCommande
PanierController:
<?php namespace Ecommerce\EcommerceBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\RedirectResponse; use Ecommerce\EcommerceBundle\Form\UtilisateursAdressesType; use Ecommerce\EcommerceBundle\Entity\UtilisateursAdresses; class PanierController extends Controller { public function menuAction() { $session = $this->getRequest()->getSession(); if (!$session->has('panier')) $articles = 0; else $articles = count($session->get('panier')); return $this->render('EcommerceBundle:Default:panier/modulesUsed/panier.html.twig', array('articles' => $articles)); } public function supprimerAction($id) { $session = $this->getRequest()->getSession(); $panier = $session->get('panier'); if (array_key_exists($id, $panier)) { unset($panier[$id]); $session->set('panier',$panier); $this->get('session')->getFlashBag()->add('success','Article successfully removed'); } return $this->redirect($this->generateUrl('panier')); } public function ajouterAction($id) { $session = $this->getRequest()->getSession(); if (!$session->has('panier')) $session->set('panier',array()); $panier = $session->get('panier'); if (array_key_exists($id, $panier)) { if ($this->getRequest()->query->get('qte') != null) $panier[$id] = $this->getRequest()->query->get('qte'); $this->get('session')->getFlashBag()->add('success','Quantity successfully modified'); } else { if ($this->getRequest()->query->get('qte') != null) $panier[$id] = $this->getRequest()->query->get('qte'); else $panier[$id] = 1; $this->get('session')->getFlashBag()->add('success','Article successfully added'); } $session->set('panier',$panier); return $this->redirect($this->generateUrl('panier')); } public function panierAction() { $session = $this->getRequest()->getSession(); if (!$session->has('panier')) $session->set('panier', array()); $em = $this->getDoctrine()->getManager(); $produits = $em->getRepository('EcommerceBundle:Produits')->findArray(array_keys($session->get('panier'))); return $this->render('EcommerceBundle:Default:panier/layout/panier.html.twig', array('produits' => $produits, 'panier' => $session->get('panier'))); } public function adresseSuppressionAction($id) { $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('EcommerceBundle:UtilisateursAdresses')->find($id); if ($this->container->get('security.context')->getToken()->getUser() != $entity->getUtilisateur() || !$entity) return $this->redirect ($this->generateUrl ('livraison')); $em->remove($entity); $em->flush(); return $this->redirect ($this->generateUrl ('livraison')); } public function livraisonAction() { $utilisateur = $this->container->get('security.context')->getToken()->getUser(); $entity = new UtilisateursAdresses(); $form = $this->createForm(new UtilisateursAdressesType(), $entity); if ($this->get('request')->getMethod() == 'POST') { $form->handleRequest($this->getRequest()); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $entity->setUtilisateur($utilisateur); $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('livraison')); } } return $this->render('EcommerceBundle:Default:panier/layout/livraison.html.twig', array('utilisateur' => $utilisateur, 'form' => $form->createView())); } public function setLivraisonOnSession() { $session = $this->getRequest()->getSession(); if (!$session->has('adresse')) $session->set('adresse',array()); $adresse = $session->get('adresse'); if ($this->getRequest()->request->get('livraison') != null && $this->getRequest()->request->get('facturation') != null) { $adresse['livraison'] = $this->getRequest()->request->get('livraison'); $adresse['facturation'] = $this->getRequest()->request->get('facturation'); } else { return $this->redirect($this->generateUrl('validation')); } $session->set('adresse',$adresse); return $this->redirect($this->generateUrl('validation')); } public function validationAction() { if ($this->get('request')->getMethod() == 'POST') $this->setLivraisonOnSession(); $em = $this->getDoctrine()->getManager(); $prepareCommande = $this->forward('EcommerceBundle:Commandes:prepareCommande'); $commande = $em->getRepository('EcommerceBundle:Commandes')->find($prepareCommande->getContent()); return $this->render('EcommerceBundle:Default:panier/layout/validation.html.twig', array('commande' => $commande)); } CommandesController:
<?php namespace Ecommerce\EcommerceBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; use Ecommerce\EcommerceBundle\Entity\UtilisateursAdresses; use Ecommerce\EcommerceBundle\Entity\Commandes; use Ecommerce\EcommerceBundle\Entity\Produits; class CommandesController extends Controller { public function facture() { $em = $this->getDoctrine()->getManager(); $generator = $this->container->get('security.secure_random'); $session = $this->getRequest()->getSession(); $adresse = $session->get('adresse'); $panier = $session->get('panier'); $commande = array(); $totalHT = 0; $totalTTC = 0; $facturation = $em->getRepository('EcommerceBundle:UtilisateursAdresses')->find($adresse['facturation']); $livraison = $em->getRepository('EcommerceBundle:UtilisateursAdresses')->find($adresse['livraison']); $produits = $em->getRepository('EcommerceBundle: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; 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() { $session = $this->getRequest()->getSession(); $em = $this->getDoctrine()->getManager(); if (!$session->has('commande')) $commande = new Commandes(); else $commande = $em->getRepository('EcommerceBundle:Commandes')->findBy(array('id' => $session->get('commande'))); $commande->setDate(new \DateTime()); $commande->setUtilisateur($this->container->get('security.context')->getToken()->getUser()); $commande->setValider(0); $commande->setReference(0); $commande->setCommande($this->facture()); if (!$session->has('commande')) { $em->persist($commande); $session->set('commande',$commande); } $em->flush(); return new Response($commande->getId()); }}
In fact, find() and findBy() return a collection of entities and not a single entity.
To directly access the entity (the first entity of the collection), just replace find() with findOne() and findBy() with findOneBy().
The error concerns the following line:
$commande = $em->getRepository('EcommerceBundle:Commandes')->findBy(array('id' => $session->get('commande')));
It was enough to call findOneBy():
$commande = $em->getRepository('EcommerceBundle:Commandes')->findOneBy(array('id' => $session->get('commande')));
To directly access the entity (the first entity of the collection), just replace find() with findOne() and findBy() with findOneBy().
The error concerns the following line:
$commande = $em->getRepository('EcommerceBundle:Commandes')->findBy(array('id' => $session->get('commande')));
It was enough to call findOneBy():
$commande = $em->getRepository('EcommerceBundle:Commandes')->findOneBy(array('id' => $session->get('commande')));