symfony - Retrieve session value and pass to an entity attribute -


i want retrieve quantity each item store in session , store in database.

how retrieve quantity in session , passed quantity attribute of article entity during database persistence?

for example article:

enter image description here

(id 4, quantity 2). store 2 in quantity attribute of article entity. tried :

$article->setquantity($session->get('panier')); 

i have error:

an exception occurred while executing 'insert article ....... {"4": "2"} notice: array string conversion

  /**      * @route("/payment", name="payment")      */     public function paymentaction(request $request)     {         $session = $request->getsession();          $produits = $this->getdoctrine()->getmanager()->getrepository('appbundle:stock')->findarray(array_keys($session->get('panier')));          $commande = $session->get('commande');          var_dump($session->get('panier'));          if ($request->ismethod('post')) {             $token = $request->get('stripetoken');              \stripe\stripe::setapikey($this->getparameter("private_key"));              \stripe\charge::create(array(                 "amount" => $commande->gettotal() * 100,                 "currency" => "eur",                 "source" => $token,                 "description" => ""             ));              foreach ($produits $produit) {                  $article = new article();                 $article->settitle($produit->getstock()->gettitle());                 $article->setcontent($produit->getstock()->getcontent());                 //problem here                 $article->setquantity($session->get('panier'));                 //                 $article->setprice($produit->getprice());                  $commande->addarticle($article);                  $em = $this->getdoctrine()->getmanager();                 $em->persist($commande);                 $em->flush();             }              return $this->redirecttoroute('confirmation');          }         return $this->render(':default:payment.html.twig', array(             'commande' => $commande,             'panier' => $session->get('panier'),             'produits' => $produits,             'public_key' => $this->getparameter("public_key"),         ));     } 

add article in session :

/**  * @route("/shop/add/{id}", name="add_article")  *  */ public function addarticlelaction(request $request, $id) {      $session = $request->getsession();      if (!$session->has('panier'))         $session->set('panier', array());     $panier = $session->get('panier');      if (array_key_exists($id, $panier)) {         if ($request->query->get('qte') != null)             $panier[$id] = $request->query->get('qte');     } else {         if ($request->query->get('qte') != null)             $panier[$id] = $request->query->get('qte');         else             $panier[$id] = 1;      }      $session->set('panier', $panier);      return $this->redirecttoroute('panier'); } 

update: if $id in addarticlelaction product id then:

     foreach ($produits $produit) {                  $article = new article();                 $article->settitle($produit->getstock()->gettitle());                 $article->setcontent($produit->getstock()->getcontent());                 //problem here                 $article->setquantity($session->get('panier')[$produit->getid()]);                 //                 $article->setprice($produit->getprice());                  $commande->addarticle($article);                  $em = $this->getdoctrine()->getmanager();                 $em->persist($commande);                 $em->flush();             } 

should work, because moment have 2 products (product1 has id 1 , product 4 has id 4). when call /shop/add/{id}, adding $session->get('panier')[1] , $session->get('panier')[4] quantities. so, when you're in foreach (to store in db), need access index 1 , index 4 ($produit->getid())


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -