Thinkphp handles multiple tables with transactions when the item is added.

Source: Internet
Author: User

 #重新父类的add方法 Public Function Add () {#同时操作多装表, you can consider using a transaction to either insert the data successfully or not plug in the input data.             #开启事务的前提是表的引擎必须是InnoDB #开启事务//mysql_query ("STRAT TRANSACTION");     #--------------(1) basic information for inserting the item------------#判断是否插件成功 if (($goods _id=parent::add ()) ===false) {return FALSE; #--------------(2) Insert Member Price------------#判断是否有POST数据提交过来 if (isset ($_post[' Ml])) {#实例化会员价格模型 $Mb _price=     M (' Memberprice ');                    #循环post提交过来的数据 foreach ($_post[' Ml ') as $k = + $v) {#插入数据 $is _ok= $Mb _price->data (Array (    ' goods_id ' = $goods _id, ' level_id ' and $k, ' price ' = ' $v, ')->add (); /*if ($is _ok===false) {mysql_query ("ROLLBACK"); return FALSE;} */}} #--------------(3) Insert commodity attribute-----------#判断POST提交过来是否有数据 if (isset ($_post[' goodsattr ')) {#     Create a property Model $good _attr=m (' goodsattr '); #循环读取post提交过来的数据 foreach ($_post[' goodsattr ') as $k = + $v) {#在判断属性提交过来的是否是一个数组 such as: Color white black golden .... if (i S_array ($v) {#在循环这个属性的数组 foreach ($v as $k 1=> $v 1) {#在循环插件数据, $is _ok= $good _attr->data (Array (       ' goods_id ' = $goods _id, ' attr_id ' + $k, ' attr_value ' + $v 1,)]->add ();  /* if ($is _ok===false) {mysql_query ("ROLLBACK");   return FALSE;  } */}}else{#说明是单个值 $is _ok= $good _attr->data (' goods_id ' = $goods _id,         ' attr_id ' = $k, ' attr_value ' + $v,)]->add ();  /* if ($is _ok===false) {mysql_query ("ROLLBACK");  return FALSE; }*/     }     }     }    }

  

Thinkphp handles multiple tables with transactions when the item is added.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.