Skip to content

Commit

Permalink
改进withAttr方法
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Dec 29, 2021
1 parent 7771698 commit 51ec287
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
9 changes: 0 additions & 9 deletions src/Model.php
Expand Up @@ -244,11 +244,6 @@ public function __construct(array $data = [])
}

$this->filter(function ($result, $options) {
// 动态获取器
if (!empty($options['with_attr'])) {
$result->withAttribute($options['with_attr']);
}

// 关联查询
if (!empty($options['relation'])) {
$result->relationQuery($options['relation'], $options['with_relation_attr']);
Expand Down Expand Up @@ -1075,10 +1070,6 @@ public function __call($method, $args)
return call_user_func_array(static::$macro[static::class][$method]->bindTo($this, static::class), $args);
}

if ('withattr' == strtolower($method)) {
return call_user_func_array([$this, 'withAttribute'], $args);
}

return call_user_func_array([$this->db(), $method], $args);
}

Expand Down
4 changes: 2 additions & 2 deletions src/db/concern/ModelRelationQuery.php
Expand Up @@ -151,8 +151,8 @@ public function withAttr($name, callable $callback = null)
$this->options['with_attr'][$name] = $callback;
}

return $this->filter(function (&$result) {
$this->getResultAttr($result, $this->options['with_attr']);
return $this->filter(function ($result) {
return $this->getResultAttr($result, $this->options['with_attr']);
}, 'with_attr');
}

Expand Down
6 changes: 4 additions & 2 deletions src/db/concern/ResultOperation.php
Expand Up @@ -108,9 +108,9 @@ protected function resultSet(array &$resultSet, bool $toCollection = true): void
* @access protected
* @param array $result 查询数据
* @param array $withAttr 字段获取器
* @return void
* @return array
*/
protected function getResultAttr(array &$result, array $withAttr = []): void
protected function getResultAttr(array $result, array $withAttr = []): array
{
foreach ($withAttr as $name => $closure) {
$name = Str::snake($name);
Expand All @@ -126,6 +126,8 @@ protected function getResultAttr(array &$result, array $withAttr = []): void
$result[$name] = $closure($result[$name] ?? null, $result);
}
}

return $result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/Collection.php
Expand Up @@ -157,7 +157,7 @@ public function setParent(Model $parent)
public function withAttr($name, $callback = null)
{
$this->each(function (Model $model) use ($name, $callback) {
$model->withAttribute($name, $callback);
$model->withAttr($name, $callback);
});

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/model/concern/Attribute.php
Expand Up @@ -657,11 +657,11 @@ protected function readTransform($value, $type)
* @param callable $callback 闭包获取器
* @return $this
*/
public function withAttribute($name, callable $callback = null)

This comment has been minimized.

Copy link
@big-dream

big-dream Dec 31, 2021

Contributor

我说怎么突然找不到withAttribute方法,真的改名了😂

public function withAttr($name, callable $callback = null)
{
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->withAttribute($key, $val);
$this->withAttr($key, $val);
}
} else {
$name = $this->getRealFieldName($name);
Expand Down

1 comment on commit 51ec287

@MoeGrid
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模型没法序列化了, 难受

Please sign in to comment.