Collection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: zhangyajun <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\model;
  12. use think\Collection as BaseCollection;
  13. use think\Model;
  14. class Collection extends BaseCollection
  15. {
  16. /**
  17. * 返回数组中指定的一列
  18. * @access public
  19. * @param string $column_key
  20. * @param string|null $index_key
  21. * @return array
  22. */
  23. public function column($column_key, $index_key = null)
  24. {
  25. return array_column($this->toArray(), $column_key, $index_key);
  26. }
  27. /**
  28. * 延迟预载入关联查询
  29. * @access public
  30. * @param mixed $relation 关联
  31. * @return $this
  32. */
  33. public function load($relation)
  34. {
  35. $item = current($this->items);
  36. $item->eagerlyResultSet($this->items, $relation);
  37. return $this;
  38. }
  39. /**
  40. * 设置需要隐藏的输出属性
  41. * @access public
  42. * @param array $hidden 属性列表
  43. * @param bool $override 是否覆盖
  44. * @return $this
  45. */
  46. public function hidden($hidden = [], $override = false)
  47. {
  48. $this->each(function ($model) use ($hidden, $override) {
  49. /** @var Model $model */
  50. $model->hidden($hidden, $override);
  51. });
  52. return $this;
  53. }
  54. /**
  55. * 设置需要输出的属性
  56. * @access public
  57. * @param array $visible
  58. * @param bool $override 是否覆盖
  59. * @return $this
  60. */
  61. public function visible($visible = [], $override = false)
  62. {
  63. $this->each(function ($model) use ($visible, $override) {
  64. /** @var Model $model */
  65. $model->visible($visible, $override);
  66. });
  67. return $this;
  68. }
  69. /**
  70. * 设置需要追加的输出属性
  71. * @access public
  72. * @param array $append 属性列表
  73. * @param bool $override 是否覆盖
  74. * @return $this
  75. */
  76. public function append($append = [], $override = false)
  77. {
  78. $this->each(function ($model) use ($append, $override) {
  79. /** @var Model $model */
  80. $model && $model->append($append, $override);
  81. });
  82. return $this;
  83. }
  84. }