Saving HABTM with extra fields?

后端 未结 6 1457
一整个雨季
一整个雨季 2020-12-01 17:32

I am trying to save an order, and the products in the order.

The order is being saved, but the products are not.

I have an orders table and a

6条回答
  •  死守一世寂寞
    2020-12-01 18:16

    You can use the previous solution, BUT if have also to update the parent tables, you must unbind. In my example I have a table Order and a table Product. I needs to put two extra fields for every product with respect to the Order.id . See this example:

        $this->Order->create(); 
        $this->Order->unBindModel(array('hasAndBelongsToMany'=>array('Product')));
        $f  = $this->Order->save($this->data,false);
    
        /* Save extra columns in HABTM TABLE */
        $this->Order->bindModel(array('hasMany'=>array('OrderProduct')));
        $q = array();
        if (!isset($this->data['Product']))
        {
            $v  = false;
        }
        else
        {
            $v =true;
    
            for($i=0;$idata['Product']);$i++)
            {
                $this->data['OrderProduct'][$i]['order_id']     = $this->Order->getLastInsertID();
                $this->data['OrderProduct'][$i]['product_id']   = $this->data['Product'][$i];
                $this->data['OrderProduct'][$i]['quantity']         = $this->data['quantity'][$i];
                $this->data['OrderProduct'][$i]['state_id']         = $this->data['State'][$i];
            }
        }
    
        $s  = $this->Order->OrderProduct->saveAll($this->data['OrderProduct']);
    

提交回复
热议问题