【问题标题】:Symfony2 creating objects with session keys and valuesSymfony2 使用会话键和值创建对象
【发布时间】:2015-08-06 10:43:11
【问题描述】:

背景

我的数据库中有两个表,CampaignList 和 CampaignProduct。逻辑很简单,如果用户成功购买产品,我必须根据购买的产品数量创建一个新的 CampaignList 和 CampaignProducts。这两个表以后会映射在一起,现在我只是在尝试正确插入。

例如,如果用户成功购买了 3 个产品,则在 CampaignList 中插入一个新表,并且 3 个新表是 CampaignProduct。

现在产品像这样存储在会话中:

11 => 2
29 => 1

key 是产品 ID,value 是数量。所以这个会话有 3 个产品,两个 id 为 11 的产品和一个 id 为 29 的产品。现在解决问题和代码。

问题

插件工作正常,除了一个。我也需要在数据库中保存产品的数量。但是我创造的这种方式我不认为我可以吗?因为我在一个不同的循环中创建表,其中数量从不迭代?这是代码

代码

if ($session->has('cart') && count($session->get('cart')) > 0) {
            // if the session is good create the new campaign
            $campaign = New CampaignList();
            $campaign->setUserId($user->getId());
            $campaign->setName('Karpedeal');
            $campaign->setState(1);
            $em->persist($campaign);
            $em->flush();

            foreach ($cart as $id => $quantity) {
                // store the product ids in an array
                $productIds[] = $id;
                //get all the products based on the id array
                $product = $em->getRepository('MpShopBundle:Product')->findById($productIds);

            }
                // for each new product create new CampaignProduct
                foreach($product as $item){

                $campaignProduct = New CampaignProduct();
                $campaignProduct->setCampaignId($campaign->getId());
                $campaignProduct->setProductId($item->getId());
                $campaignProduct->setSellingPrice($item->getPrice());
                $campaignProduct->setStock($item->getStockAvailable());
                $campaignProduct->setReserved($quantity); // PROBLEM how to get the quantity from the session??
                $em->persist($campaignProduct);
                $em->flush();
                }

另一种方式

我认为我能做的唯一方法是在第一个 foreach 循环中做所有事情,但是当我尝试获取产品的 id 时,我会遇到错误,因为它们不是对象,而是数组......

if ($session->has('cart') && count($session->get('cart')) > 0) {
            // if the session is good create the new campaign
            $campaign = New CampaignList();
            $campaign->setUserId($user->getId());
            $campaign->setName('Karpedeal');
            $campaign->setState(1);
            $em->persist($campaign);
            $em->flush();

            foreach ($cart as $id => $quantity) {
                // store the product ids in an array
                $productIds[] = $id;
                //get all the products based on the id array
                $product = $em->getRepository('MpShopBundle:Product')->findById($productIds);

                $campaignProduct = New CampaignProduct();
                $campaignProduct->setCampaignId($campaign->getId());
                $campaignProduct->setProductId($product->getId()); // the error here because $product is an array not object
                $campaignProduct->setSellingPrice($item->getPrice());
                $campaignProduct->setStock($product->getStockAvailable());
                $campaignProduct->setReserved($quantity); // PROBLEM how to get the quantity from the session??
                $em->persist($campaignProduct);
                $em->flush();
                }

有什么想法吗?

【问题讨论】:

    标签: php mysql symfony session


    【解决方案1】:

    通过查看您的代码,我想您的第二个答案是最好的,但不是让所有产品都具有一系列产品 ID,而是每次运行只获取一个产品,这会导致您的产品成为 MpShopBundle:Product 的实例。

    tl;博士

    改变这个 $product = $em->getRepository('MpShopBundle:Product')->findById($productIds);

    进入这个 $product = $em->getRepository('MpShopBundle:Product')->find($id);

    应该有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2016-01-26
      • 2023-03-15
      • 2021-08-25
      相关资源
      最近更新 更多