Skip to content

Commit

Permalink
compatible group count
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuraovq committed Nov 21, 2019
1 parent e57c804 commit ab58ffd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/db/src/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public function syncOriginal(): self
/**
* Sync a single original attribute with its current value.
*
* @param string $attribute
* @param string|array $attribute
*
* @return $this
*/
Expand Down
5 changes: 2 additions & 3 deletions src/db/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public function syncCounter(array $counters, array $extra = []): self

if ($extra) {
// Sync extra
$this->fill($extra);
$this->setRawAttributes($extra, true);
}

return $this;
Expand Down Expand Up @@ -604,8 +604,7 @@ protected function setKeysForSaveQuery(Builder $query)
*/
protected function getKeyForSaveQuery()
{
return $this->modelOriginal[$this->getKeyName()]
?? $this->getKey();
return $this->modelOriginal[$this->getKeyName()] ?? $this->getKey();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/db/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,11 @@ public function aggregate(string $function, array $columns = ['*'])
->get($columns);

if (!$results->isEmpty()) {
// Compatible group aggregate
if (isset($this->groups)) {
return $results->pluck('aggregate')->$function();
}

return array_change_key_case((array)$results[0])['aggregate'];
}

Expand Down Expand Up @@ -3073,6 +3078,11 @@ public function warpCounters(array $counters): array
{
// Convert counters to expression
foreach ($counters as $column => $value) {
if (empty($value)) {
unset($counters[$column]);
continue;
}

if (!$value instanceof Expression) {
$wrapped = $this->grammar->wrap($column);

Expand Down
67 changes: 66 additions & 1 deletion src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,9 @@ public function testUpdateAllCounters()
$this->assertEquals($user->getAge() - 1, User::find($id)->getAge());

$user = User::find($id);
$user->updateCounters(['age' => -1]);
$user->updateCounters(['age' => -1], ['udesc' => 'swoft']);

$this->assertEquals([], $user->getDirty());
$this->assertEquals($user->getAge(), User::find($id)->getAge());
}

Expand Down Expand Up @@ -877,4 +878,68 @@ public function testFindOrFail()
$this->expectException(DbException::class);
$user->update(['age' => 2]);
}

public function testDirty(): void
{
$origin = ['age' => 1, 'name' => 'swoft'];
User::updateOrCreate(['id' => 1], $origin);

$user = User::find(1);

$dirty = $user->getDirty();
// No changes
$this->assertEquals([], $dirty);


$user->setAge(2);
$dirty = $user->getDirty();
// Change age to 2
$this->assertEquals(['age' => 2], $dirty);


$user->setName('swoft2');
$dirty = $user->getDirty();
// Change name to swoft2
$this->assertEquals(['age' => 2, 'name' => 'swoft2'], $dirty);


// recovery changes
$fillOrigin = $user->fill($origin)->getDirty();
$this->assertEquals([], $fillOrigin);

// setter
$user->setModelAttribute('name', 'on');
$dirty = $user->getDirty();
$this->assertEquals(['name' => 'on'], $dirty);
}

public function testGroupAggregate(): void
{
User::truncate();

$origin = ['age' => 1, 'user_desc' => 'swoft'];
$origin2 = ['age' => 2, 'user_desc' => 'swoft2'];

User::updateOrCreate(['id' => 1], $origin);
User::updateOrCreate(['id' => 2], $origin2);
User::updateOrCreate(['id' => 3], $origin2);

$count = User::groupBy('user_desc')->count();

$this->assertEquals(2, $count);

$originCount = User::count();
$this->assertEquals(3, $originCount);

$minAge = User::groupBy('age')->min('age');
$this->assertEquals(1, $minAge);
$this->assertEquals(1, User::min('age'));

$maxAge = User::groupBy('age')->max('age');
$this->assertEquals(2, $maxAge);
$this->assertEquals(2, User::max('age'));

$this->assertEquals(1.6667, User::avg('age'));
$this->assertEquals(1.5, User::groupBy('age')->avg('age'));
}
}

0 comments on commit ab58ffd

Please sign in to comment.