TestModel.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. *
  4. *User:Administrator
  5. *Date:2021/10/11
  6. */
  7. namespace app\api\model;
  8. use think\Model;
  9. class TestModel extends Model
  10. {
  11. protected $table = 't_test_model';
  12. protected $pk = 'id';
  13. //关联模型一对多定义
  14. public function testProduct()
  15. {
  16. return $this->hasMany(TestProduct::class, 'test_model_id', 'id');
  17. }
  18. //关联模型一对多定义
  19. public function testPesticides()
  20. {
  21. return $this->hasMany(TestPesticides::class, 'test_model_id', 'id');
  22. }
  23. //关联模型一对多使用
  24. public function getProductInfo($model_id,$where='')
  25. {
  26. $models = TestModel::find($model_id);
  27. $productInfo = $models->testProduct()->where($where)->select();
  28. return $productInfo;
  29. }
  30. //关联模型一对多使用
  31. public function getPesticidesInfo($model_id,$where='')
  32. {
  33. $models = TestModel::find($model_id, $where = '');
  34. $pesticides = $models->testPesticides()->where($where)->select();
  35. return $pesticides;
  36. }
  37. /**
  38. * @param $uid
  39. * @param int $pageNum
  40. * @param int $pageSize
  41. * @param array $where:type_id为int
  42. */
  43. public function getListByUserId($uid,$pageNum=1,$pageSize=10,$where=[])
  44. {
  45. return $this->where('user_id','=',(int)$uid)->where($where)->page($pageNum,$pageSize)->select();
  46. }
  47. public function countListByUserId($uid, $where = [])
  48. {
  49. return $this->where('user_id','=',(int)$uid)->where($where)->count();
  50. }
  51. public function getModelInfoByModelID($model_id)
  52. {
  53. return $this->find($model_id);
  54. }
  55. public function updateModelInfoByModelID($model_id,$update_data)
  56. {
  57. return $this->find($model_id)->save($update_data);
  58. }
  59. public function createModelInfo($model_name,$type_id,$user_id)
  60. {
  61. return $this->insertGetId([
  62. 'name'=>$model_name,
  63. 'type_id'=>$type_id,
  64. 'user_id'=>$user_id
  65. ]);
  66. }
  67. public function getUserIdByModelID($model_id)
  68. {
  69. if (empty($model_id)) {
  70. return false;
  71. }
  72. return $this->where('id', '=', $model_id)->field('user_id')->find();
  73. }
  74. public function deleteModelInfoByID($model_id)
  75. {
  76. if (empty($model_id)) {
  77. return false;
  78. }
  79. return $this->where('id', '=', $model_id)->delete();
  80. }
  81. }