Skip to content

Commit

Permalink
Merge branch 'feature/groupby'
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Jun 11, 2021
2 parents 8320a60 + ff6be72 commit 3a5b9e9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function where($sql, $data)
return $this;
}

public function groupBy($sql)
{
$this->ensureFrom();
$this->sql .= ' GROUP BY ' . $sql;
return $this;
}

public function orderBy($sql)
{
$this->ensureFrom();
Expand Down
36 changes: 36 additions & 0 deletions test/anorm/DataMapper_Find_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;

use Anorm\DataMapper;
use Anorm\Test\SomeTableAggregateModel;
use Anorm\Test\SomeTableModel;
use Anorm\Test\TestEnvironment;

Expand All @@ -31,6 +32,7 @@ public static function setUpBeforeClass()
for ($i = 0; $i < 10; ++$i) {
$model->name = "Name $i";
$model->someId = null;
$model->category = floor($i / 4);
$model->write();
}
}
Expand Down Expand Up @@ -97,4 +99,38 @@ public function testFindOneOrThrow_NotPresent_Throws()
->oneOrThrow();
}

public function testFindSome_GroupBy_OK()
{
/** @var SomeTableAggregateModel[] */
$generator = DataMapper::find(SomeTableAggregateModel::class, $this->pdo)
->select("COUNT(s.category) AS category_count,MIN(s.some_id) AS id_min,MAX(s.some_id) AS id_max")
->from("some_table AS s")
->groupBy("category")
->some();
$result = [];
foreach ($generator as $model) {
$result[] = [
'count' => $model->categoryCount,
'min' => $model->idMin,
'max' => $model->idMax,
];
}
$this->assertEquals(3, count($result));
$this->assertEquals([
'count' => '4',
'min' => '1',
'max' => '4',
], $result[0]);
$this->assertEquals([
'count' => '4',
'min' => '5',
'max' => '8',
], $result[1]);
$this->assertEquals([
'count' => '2',
'min' => '9',
'max' => '10',
], $result[2]);
}

}
2 changes: 2 additions & 0 deletions test/anorm/QueryBuilder_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function testFunctions_ReturnThis()
$this->assertSame($o, $result);
$result = $o->where('', []);
$this->assertSame($o, $result);
$result = $o->groupBy('');
$this->assertSame($o, $result);
$result = $o->orderBy('');
$this->assertSame($o, $result);
$result = $o->limit('');
Expand Down
21 changes: 21 additions & 0 deletions test/anorm/SomeTableAggregateModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Anorm\Test;

use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Transformer;

class SomeTableAggregateModel extends Model {
public function __construct()
{
$pdo = Anorm::pdo();
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->table = 'some_table';
}

public $categoryCount;
public $idMin;
public $idMax;
}

1 change: 1 addition & 0 deletions test/anorm/SomeTableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function countRows()

public $someId;
public $name;
public $category;
public $dtc;
}

1 change: 1 addition & 0 deletions test/anorm/TestSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
CREATE TABLE `some_table` (
`some_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(128) NOT NULL,
`category` varchar(16) NULL,
`dtc` date NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

0 comments on commit 3a5b9e9

Please sign in to comment.