CabinetModel.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. use app\index\model\BoxModel;
  5. use think\exception\PDOException;
  6. class CabinetModel extends Model{
  7. // 确定链接表名
  8. protected $name = 'cabinet';
  9. /**
  10. * 插入柜子信息
  11. * @param $param
  12. */
  13. public function insertCabinet($url,$alias,$count)
  14. {
  15. try
  16. {
  17. $this->startTrans();
  18. $result = $this->save(['req_url' => $url,'alias' => $alias,'box_count' => $count]);
  19. if(false === $result){
  20. $this->rollback();
  21. return msg(-1, '', $this->getError());
  22. }
  23. else
  24. {
  25. $cabinet_number = intval($this->id);
  26. $boxData = [];
  27. for ($i=1; $i <= $count; $i++) {
  28. $boxData['cabinet_number'] = $cabinet_number;
  29. $boxData['box_number'] = $i;
  30. $saveAllData[] = $boxData;
  31. }
  32. $BoxModel = new BoxModel();
  33. $result = $BoxModel->saveAll($saveAllData);
  34. if(false === $result){
  35. $this->rollback();
  36. return msg(-1, '', $this->getError());
  37. }else{
  38. $this->commit();
  39. return msg(1, '', '添加成功');
  40. }
  41. }
  42. }
  43. catch(PDOException $e){
  44. $this->rollback();
  45. return msg(-2, '', $e->getMessage());
  46. }
  47. }
  48. }