PortalCategoryModel.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\model;
  10. use think\Model;
  11. class PortalCategoryModel extends Model
  12. {
  13. //类型转换
  14. protected $type = [
  15. 'more' => 'array',
  16. ];
  17. //模型关联方法
  18. protected $relationFilter = ['articles'];
  19. /**
  20. * more 自动转化
  21. * @param $value
  22. * @return array
  23. */
  24. public function getMoreAttr($value)
  25. {
  26. $more = json_decode($value, true);
  27. if (!empty($more['thumbnail'])) {
  28. $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  29. }
  30. if (!empty($more['photos'])) {
  31. foreach ($more['photos'] as $key => $value) {
  32. $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  33. }
  34. }
  35. return $more;
  36. }
  37. /**
  38. * 关联文章表
  39. * @return \think\model\relation\BelongsToMany
  40. */
  41. public function articles()
  42. {
  43. return $this->belongsToMany('PortalPostModel', 'portal_category_post', 'post_id', 'category_id');
  44. }
  45. /**
  46. * 关联文章分类和文章表
  47. * @return \think\model\relation\HasMany
  48. */
  49. public function PostIds()
  50. {
  51. return $this->hasMany('PortalCategoryPostModel', 'category_id', 'id');
  52. }
  53. /**
  54. * 此类文章id数组
  55. * @param string $category_id 分类di
  56. * @return array|string|Model|null
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @throws \think\exception\DbException
  60. */
  61. public static function categoryPostIds($category_id)
  62. {
  63. $ids = [];
  64. $post_ids = self::relation('PostIds')->field(true)->where('id', $category_id)->find();
  65. foreach ($post_ids['PostIds'] as $key => $id) {
  66. $ids[] = $id['post_id'];
  67. }
  68. $post_ids['PostIds'] = $ids;
  69. return $post_ids;
  70. }
  71. }