2016-04-07 4 views
0

ランダムな試行回数の後にremoveAction関数が機能する理由がわかりません。ときには、それが動作するまで、最初と他の(4または5回)を何度も何度もクリックしなければならないことがあります。ランダムな試行回数の後に関数ロジックが機能する

商品IDがユーザーのカートから削除されたため、正常に動作しています。

私はこのことができますかはわからないが、私は(float(0.0148420333862)ある)機能のmicrotime()に対して(30です)max_execution_timeにチェック。

私が$quantity$cartをダンプすると、常に正しい製品IDが表示されます。

removeAction function

/** 
    * Removes a 'product' from the cart 
    * 
    * @Route("/{id}/remove", name="product_remove") 
    * @METHOD("GET") 
    * @Template() 
    */ 
    public function removeAction(Request $request, $id) { 

     // $time = microtime(true); 

     $em = $this->getDoctrine()->getManager(); 

     $product = $em->getRepository('ShopBundle:Product')->find($id); 

     $cart = $em->getRepository('ShopBundle:UserCart')->findOneBy(['user' => $this->getUser(), 'submitted' => false]); 

     $quantity = $em->getRepository('ShopBundle:Quantity')->findOneBy(['product' => $product->getId()]); 
               //get product id from the cart and then remove it 

    // product gets removed but only after a random # of click on the remove button... 

//ini_get('max_execution_time'); 
//var_dump(ini_get('max_execution_time')); 

     // dump($quantity); 
     // dump($cart); 


     $em->remove($quantity); 
     $em->flush(); 

     $this->addFlash('notice', 'The product: '.$product->getName().' was removed!'); 

     //var_dump(microtime(true) - $time); die; 

     return $this->redirectToRoute('product_showCart'); 
    } 
+0

これは奇妙なやり方です。私はそれがどのように働くのか分からないのですか?確かに、カートとその中の製品との関係はありますか?同じ商品が複数のカートにある場合はどうなりますか?そのコードはどのカートから取り除くべきかをどのように知っていますか?あなたの方法は私がそれを読むときにはあまり意味がありません。私はこれが標準的な方法でうまくいくと思います。 – Richard

+0

これはおそらくこの笑いを行う奇妙な方法です。私は新しいですが、私は学んでいます。私はこれを有効にすることができましたが、これを修正しました: '$ quantity = $ em-> getRepository( 'ShopBundle:Quantity') - > findOneBy(['product' => $ product-> getId()])' :$ item = $ em-> getRepository( 'ShopBundle:Quantity') - > findOneBy(['product' => $ product-> getId()、 'userCart' => $ cart-> getId()]); ' – Ale

+0

それは良く見えます:) – Richard

答えて

1

は、ここで私はあなたのコードに関するいくつかの仮定を行う例ですが、それは、私は物事が動作することを期待したい方法を大まかにだとずっと簡単。

あなたはカートを検査し、一致するアイテムを見つけてそれを取り除くことができるはずです。そうでなければ、カートがどのように構築されているかをリファクタリングするべきです。

私は、あなたの元のコードが一連のクリックの後に働いた理由は、それがあなたのカートに届くまで、それが入っていたすべてのカートから製品を削除したことを推測します。

/** 
* Removes a 'product' from the cart 
* 
* @Route("/{id}/remove", name="product_remove") 
* @METHOD("GET") 
* @Template() 
*/ 
public function removeAction(Request $request, $id) { 

    $em = $this->getDoctrine()->getManager(); 
    $cart = $em->getRepository('ShopBundle:UserCart')->findOneBy(['user' => $this->getUser(), 'submitted' => false]); 

    // Surely you must have some sort of cart->product or cart -> cartitem -> product relation here - I make some assumptions but what you 
    // do should ideally work similar to this. 

    foreach ($cart->getItems() as $item) { 

     if ($item->getProduct()->getId() == $id) { 

      $em->remove($item); 
      $em->flush(); 
      $this->addFlash('notice', 'The product was removed!'); 
      return $this->redirectToRoute('product_showCart'); 
     } 
    } 

    // here you could put some kind of error because you failed to remove the product   
} 
+0

私の解決策を見つけて数時間後、私はコントローラの別の機能でこれに似た何かをしました!しかし、あなたの入力に感謝 – Ale

関連する問題