Adding data to jointable in a loop after adding data to one of the joint models Cakephp2

So im making an online shop where i want to make each purchase have multiple products. I have the tables products, purchases and product_purchases. I want this function to add a row to purchases (that part works), substract the required stock (also works) and add the number of rows needed to product_purchases. The problem is that only the last row gets saved into the join table.

public function add($price=null){

        $user_id = $this->Session->read('Auth.User')['id'];
        
        $purchase_in = array();
        $purchase_in['Purchase']['user_id']= $user_id;
        $purchase_in['Purchase']['price']= $price;
        $this->Purchase->save($purchase_in);

        $jointable_in = array();
        $Purchase = array();

        $purchase_id=$this->Purchase->getInsertID();

        $Purchase['id_purchase']= $purchase_id;

        $jointable_in['Purchase']['Purchase']= $Purchase;

        $cart = $this->Session->read('cart');


        foreach ($cart as $product_id) {
            $product = $this->Purchase->Product->findById($product_id);

            $stock = ($product['Product']['stock'])-1;

            $stock_in = array();
            $stock_in['Product']['id']= $product_id;
            $stock_in['Product']['stock']= $stock;
            $this->Purchase->Product->save($stock_in);

            
            $product = array();

            $product['id_product']= $product_id;
            $jointable_in['Product']['Product']= $product;
            
            $this->Product->Purchase->save($jointable_in);
            
        }
        return $this->redirect(array('controller'=>'products','action' => 'cartempty'));

    }

Ive tried saving into ProductsPurchase model but doesnt work, ive also tried inputting an array like in the cookbook to saveAll and it doesnt save anything, ive also tried to do this $this->Product->Purchase->clear() after each save but it adds empty rows to the purchases table so i dont know what else to try.

Upvotes: 0

Views: 39

Answers (1)

I have finally solved this, i just had to use saveAll() function once.

In my code i tried to save the purchase first and the HABTM table data after that. The main problem was that by doing it that way cake was treating them as separate records.

In the end i included the data from the first save in the array for the second one and saved it outside the loop.

public function add($price=null){ $idusuario = $this->Session->read('Auth.User')['id'];

    $jointable_in = array();
    $aux_array = array();

    $Purchase = array();
    $Purchase['user_id']= $idusuario;
    $Purchase['price']= $price;

    $aux_array['Purchase']= $Purchase;

    $products = array();
    $carrito = $this->Session->read('cart');
    $i=0;

    foreach ($carrito as $idproducto) {
        $producto = $this->Purchase->Product->findById($idproducto);
        $stock = ($producto['Product']['stock'])-1;
        $sales = ($producto['Product']['sales'])+1;

        $stock_in = array();
        $stock_in['Product']['id']= $idproducto;
        $stock_in['Product']['stock']= $stock;
        $stock_in['Product']['sales']= $sales;
        //$this->Purchase->Product->save($stock_in);

        $shop = $this->Product->Shop->findById($producto['Product']['shop_id']);
        $shop_in = array();
        $shop_in['Shop']['id']= $shop['Shop']['id'];
        $shop_sales = ($shop['Shop']['sales'])+1;
        $shop_in['Shop']['sales']= $shop_sales;

        //$this->Product->Shop->save($shop_in);


        $products[$i]=$idproducto;
        $i++;
    }

    $aux_array['Purchase']['count']=$i;

    $aux_array['Product']['Product']= $products;
    array_push($jointable_in,$aux_array);

    //exit(debug($jointable_in));

    $this->Purchase->saveAll($jointable_in,array('deep' => true));

    return $this->redirect(array('controller'=>'products','action' => 'cartempty'));
}

Upvotes: 0

Related Questions