Productcategory.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\admin\model\wwh;
  3. use think\Model;
  4. class Productcategory extends Model
  5. {
  6. // 表名
  7. protected $name = 'wwh_productcategory';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = false;
  10. // 定义时间戳字段名
  11. protected $createTime = false;
  12. protected $updateTime = false;
  13. // 追加属性
  14. protected $append = [
  15. ];
  16. protected static function init()
  17. {
  18. self::beforeInsert(function ($row) {
  19. $pids = 0;
  20. if ($row->pid != 0) {
  21. $pids = self::getParentIds($row->pid);
  22. $pids .= "," . $row->pid;
  23. }
  24. $row->pids = $pids;
  25. });
  26. self::beforeUpdate(function ($row) {
  27. $changeData = $row->getChangedData();
  28. if (isset($changeData['pid'])) {
  29. $row->pids = self::getParentIds($row->pid) . ',' . $row->pid;
  30. }
  31. });
  32. }
  33. public static function getParentIds($pid) {
  34. $row = self::get($pid);
  35. if ( !$row ) {
  36. return 0;
  37. }
  38. return $row->pids;
  39. }
  40. }