赛亿官网

Collection.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. * @param string $column_key
  19. * @param string|null $index_key
  20. * @return array
  21. */
  22. public function column($column_key, $index_key = null)
  23. {
  24. if (function_exists('array_column')) {
  25. return array_column($this->toArray(), $column_key, $index_key);
  26. }
  27. return parent::column($column_key, $index_key);
  28. }
  29. /**
  30. * 延迟预载入关联查询
  31. * @access public
  32. * @param mixed $relation 关联
  33. * @return $this
  34. */
  35. public function load($relation)
  36. {
  37. $item = current($this->items);
  38. $item->eagerlyResultSet($this->items, $relation);
  39. return $this;
  40. }
  41. /**
  42. * 设置需要隐藏的输出属性
  43. * @access public
  44. * @param array $hidden 属性列表
  45. * @param bool $override 是否覆盖
  46. * @return $this
  47. */
  48. public function hidden($hidden = [], $override = false)
  49. {
  50. $this->each(function ($model) use ($hidden, $override) {
  51. /** @var Model $model */
  52. $model->hidden($hidden, $override);
  53. });
  54. return $this;
  55. }
  56. /**
  57. * 设置需要输出的属性
  58. * @param array $visible
  59. * @param bool $override 是否覆盖
  60. * @return $this
  61. */
  62. public function visible($visible = [], $override = false)
  63. {
  64. $this->each(function ($model) use ($visible, $override) {
  65. /** @var Model $model */
  66. $model->visible($visible, $override);
  67. });
  68. return $this;
  69. }
  70. /**
  71. * 设置需要追加的输出属性
  72. * @access public
  73. * @param array $append 属性列表
  74. * @param bool $override 是否覆盖
  75. * @return $this
  76. */
  77. public function append($append = [], $override = false)
  78. {
  79. $this->each(function ($model) use ($append, $override) {
  80. /** @var Model $model */
  81. $model->append($append, $override);
  82. });
  83. return $this;
  84. }
  85. }