Dominykas55
Dominykas55

Reputation: 1231

Symfony2 creating objects with session keys and values

BACKGROUND

I have two tables in my database, CampaignList and CampaignProduct. The logic is simple, if the user successfuly purchases products I have to create one new CampaignList and CampaignProducts based for the number of products bought. These two tables will be mapped together in the future, right now I am just trying to insert them correctly.

So for example if a user successfuly buys 3 products one new table is inserted in CampaignList and 3 new tables is CampaignProduct.

Now the products are storred in session like this:

11 => 2
29 => 1

The key is the id of the product and the value is the quantity. So this session has 3 products, two products with the id of 11 and one products with the id of 29. Now for the problem and the code.

THE PROBLEM

The inserts are working correctly, except one. I need to save the quantity of the product too in the database. But this way that I am creating I dont think i can? Because I am creating tables in a different loop where the quantity is never iterated? Here is the code

THE CODE

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();
                }

THE OTHER WAY

The only way I think I can do is do everything in the first foreach loop, however that way I am getting an error when I try to get the ids of the products, because they are not objects, but arrays...

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();
                }

Any ideas?

Upvotes: 2

Views: 60

Answers (1)

Ricardo
Ricardo

Reputation: 522

By looking at your code I guess your second answer is best, but instead of getting all products with an array of product ids, just get one product every run which results in your product to be an instance of MpShopBundle:Product.

tl;dr

changing this $product = $em->getRepository('MpShopBundle:Product')->findById($productIds);

into this $product = $em->getRepository('MpShopBundle:Product')->find($id);

Should work

Upvotes: 3

Related Questions