123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: zhangyajun <448901948@qq.com>
- // +----------------------------------------------------------------------
- namespace think\model;
- use think\Collection as BaseCollection;
- use think\Model;
- class Collection extends BaseCollection
- {
- /**
- * 延迟预载入关联查询
- * @access public
- * @param mixed $relation 关联
- * @return $this
- */
- public function load($relation)
- {
- if (!$this->isEmpty()) {
- $item = current($this->items);
- $item->eagerlyResultSet($this->items, $relation);
- }
- return $this;
- }
- /**
- * 绑定(一对一)关联属性到当前模型
- * @access protected
- * @param string $relation 关联名称
- * @param array $attrs 绑定属性
- * @return $this
- */
- public function bindAttr($relation, array $attrs = [])
- {
- $this->each(function (Model $model) use ($relation, $attrs) {
- $model->bindAttr($relation, $attrs);
- });
- return $this;
- }
- /**
- * 设置需要隐藏的输出属性
- * @access public
- * @param array $hidden 属性列表
- * @param bool $override 是否覆盖
- * @return $this
- */
- public function hidden($hidden = [], $override = false)
- {
- $this->each(function ($model) use ($hidden, $override) {
- /** @var Model $model */
- $model->hidden($hidden, $override);
- });
- return $this;
- }
- /**
- * 设置需要输出的属性
- * @access public
- * @param array $visible
- * @param bool $override 是否覆盖
- * @return $this
- */
- public function visible($visible = [], $override = false)
- {
- $this->each(function ($model) use ($visible, $override) {
- /** @var Model $model */
- $model->visible($visible, $override);
- });
- return $this;
- }
- /**
- * 设置需要追加的输出属性
- * @access public
- * @param array $append 属性列表
- * @param bool $override 是否覆盖
- * @return $this
- */
- public function append($append = [], $override = false)
- {
- $this->each(function ($model) use ($append, $override) {
- /** @var Model $model */
- $model && $model->append($append, $override);
- });
- return $this;
- }
- /**
- * 设置数据字段获取器
- * @access public
- * @param string|array $name 字段名
- * @param callable $callback 闭包获取器
- * @return $this
- */
- public function withAttr($name, $callback = null)
- {
- $this->each(function ($model) use ($name, $callback) {
- /** @var Model $model */
- $model && $model->withAttribute($name, $callback);
- });
- return $this;
- }
- }
|