Collection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 mixed $relation 关联
  20. * @return $this
  21. */
  22. public function load($relation)
  23. {
  24. if (!$this->isEmpty()) {
  25. $item = current($this->items);
  26. $item->eagerlyResultSet($this->items, $relation);
  27. }
  28. return $this;
  29. }
  30. /**
  31. * 绑定(一对一)关联属性到当前模型
  32. * @access protected
  33. * @param string $relation 关联名称
  34. * @param array $attrs 绑定属性
  35. * @return $this
  36. */
  37. public function bindAttr($relation, array $attrs = [])
  38. {
  39. $this->each(function (Model $model) use ($relation, $attrs) {
  40. $model->bindAttr($relation, $attrs);
  41. });
  42. return $this;
  43. }
  44. /**
  45. * 设置需要隐藏的输出属性
  46. * @access public
  47. * @param array $hidden 属性列表
  48. * @param bool $override 是否覆盖
  49. * @return $this
  50. */
  51. public function hidden($hidden = [], $override = false)
  52. {
  53. $this->each(function ($model) use ($hidden, $override) {
  54. /** @var Model $model */
  55. $model->hidden($hidden, $override);
  56. });
  57. return $this;
  58. }
  59. /**
  60. * 设置需要输出的属性
  61. * @access public
  62. * @param array $visible
  63. * @param bool $override 是否覆盖
  64. * @return $this
  65. */
  66. public function visible($visible = [], $override = false)
  67. {
  68. $this->each(function ($model) use ($visible, $override) {
  69. /** @var Model $model */
  70. $model->visible($visible, $override);
  71. });
  72. return $this;
  73. }
  74. /**
  75. * 设置需要追加的输出属性
  76. * @access public
  77. * @param array $append 属性列表
  78. * @param bool $override 是否覆盖
  79. * @return $this
  80. */
  81. public function append($append = [], $override = false)
  82. {
  83. $this->each(function ($model) use ($append, $override) {
  84. /** @var Model $model */
  85. $model && $model->append($append, $override);
  86. });
  87. return $this;
  88. }
  89. /**
  90. * 设置数据字段获取器
  91. * @access public
  92. * @param string|array $name 字段名
  93. * @param callable $callback 闭包获取器
  94. * @return $this
  95. */
  96. public function withAttr($name, $callback = null)
  97. {
  98. $this->each(function ($model) use ($name, $callback) {
  99. /** @var Model $model */
  100. $model && $model->withAttribute($name, $callback);
  101. });
  102. return $this;
  103. }
  104. }