TestModel.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public function getListByUserId($uid,$pageNum=1,$pageSize=10,$where=[])
  38. {
  39. return $this->where('user_id','=',(int)$uid)->where($where)->page($pageNum,$pageSize)->select();
  40. }
  41. public function countListByUserId($uid, $where = [])
  42. {
  43. return $this->where('user_id','=',(int)$uid)->where($where)->count();
  44. }
  45. public function getModelInfoByModelID($model_id)
  46. {
  47. return $this->find($model_id);
  48. }
  49. public function updateModelInfoByModelID($model_id,$update_data)
  50. {
  51. return $this->find($model_id)->save($update_data);
  52. }
  53. public function createModelInfo($model_name,$type_id,$user_id)
  54. {
  55. return $this->insertGetId([
  56. 'name'=>$model_name,
  57. 'type_id'=>$type_id,
  58. 'user_id'=>$user_id
  59. ]);
  60. }
  61. public function getUserIdByModelID($model_id)
  62. {
  63. if (empty($model_id)) {
  64. return false;
  65. }
  66. return $this->where('id', '=', $model_id)->field('user_id')->find();
  67. }
  68. public function deleteModelInfoByID($model_id)
  69. {
  70. if (empty($model_id)) {
  71. return false;
  72. }
  73. return $this->where('id', '=', $model_id)->delete();
  74. }
  75. }