Product.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Product extends Model
  5. {
  6. protected $pk = 'product_id';
  7. public function porder()
  8. {
  9. return $this->hasMany('Order', 'product_id', 'product_id');
  10. }
  11. //模糊查询时使用,返回地商品的ID数组
  12. public function getIdsByNameLike($name)
  13. {
  14. $return = [];
  15. $ret = $this->field('product_id')->where(['product_name' => ['like', "%$name%"]])->select();
  16. if ($ret) {
  17. foreach ($ret as $val) {
  18. $return[] = $val->product_id;
  19. }
  20. }
  21. return $return;
  22. }
  23. public function getSonList($condition)
  24. {
  25. $result = $this
  26. ->where($condition)
  27. ->paginate(10);
  28. return $result;
  29. }
  30. public function getFartherList()
  31. {
  32. $result = $this
  33. ->where(['product_pid' => 0])
  34. ->select();
  35. return $result;
  36. }
  37. public function findProduct($condition)
  38. {
  39. $result = $this
  40. ->where($condition)
  41. ->find();
  42. return $result;
  43. }
  44. public function updateProduct($id, $data)
  45. {
  46. $result = $this
  47. ->where(['product_id' => $id])
  48. ->update($data);
  49. return $result;
  50. }
  51. public function addProduct($data)
  52. {
  53. $result = $this
  54. ->insert($data);
  55. return $result;
  56. }
  57. public function deleteProduct($condition)
  58. {
  59. $result = $this
  60. ->where($condition)
  61. ->delete();
  62. return $result;
  63. }
  64. }