/** * 添加定制 * * @param int $user_id * 用户ID * @param string $name * 定制名称 * @param string $goods_list * 商品列表 * @return array */ public function addCustom($user_id, $name, $goods_list) { if ($this->where(array('user_id' => $user_id, 'name' => $name, 'is_delete' => 0))->count()) { return array('status' => 0, 'result' => '定制名称不能重复'); } $now = time(); // 开启事务 $this->startTrans(); if ($this->add(array('user_id' => $user_id, 'name' => $name, 'create_time' => $now))) { $custom_id = $this->getLastInsID(); $goods_list = ob2ar(json_decode($goods_list)); $dataList = array(); foreach ($goods_list as $v) { $dataList[] = array('custom_id' => $custom_id, 'goods_id' => $v['goods_id'], 'quantity' => $v['quantity'], 'add_time' => $now); } if (M('CustomGoods')->addAll($dataList)) { // 添加成功,提交事务 $this->commit(); return array('status' => 1, 'result' => '添加成功'); } else { // 添加失败,回滚事务 $this->rollback(); return array('status' => 0, 'result' => '添加失败'); } } else { // 添加失败,回滚事务 $this->rollback(); return array('status' => 0, 'result' => '添加失败'); } }
/** * 对象转换成数组 * * @param object $obj * 对象 * @return array */ function ob2ar($obj) { if (is_object($obj)) { $obj = (array) $obj; $obj = ob2ar($obj); } elseif (is_array($obj)) { foreach ($obj as $key => $value) { $obj[$key] = ob2ar($value); } } return $obj; }