Skip to content

Commit

Permalink
ActiveRecord::all()
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 11, 2024
1 parent 7bb4556 commit 6d6c020
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,17 @@ public function deleteByQuery(DeleteQuery $updatable): bool
* @param bool $forUpdate
* @return array
*/
public function getByFilter(string|IteratorFilter $filter, array $params = [], bool $forUpdate = false, int $page = 0, ?int $limit = null): array
public function getByFilter(string|IteratorFilter $filter = "", array $params = [], bool $forUpdate = false, int $page = 0, ?int $limit = null): array
{
if ($filter instanceof IteratorFilter) {
$formatter = new IteratorFilterSqlFormatter();
$filter = $formatter->getFilter($filter->getRawFilters(), $params);
}


$query = $this->getMapper()->getQuery()
->where($filter, $params);
$query = $this->getMapper()->getQuery();
if (!empty($filter)) {
$query->where($filter, $params);
}

if ($forUpdate) {
$query->forUpdate();
Expand Down
7 changes: 7 additions & 0 deletions src/Trait/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ public static function get(mixed ...$pk)

/**
* @param IteratorFilter $filter
* @param int $page
* @param int $limit
* @return static[]
*/
public static function filter(IteratorFilter $filter, int $page = 0, int $limit = 50): array
{
return self::$repository->getByFilter($filter, page: $page, limit: $limit);
}

public static function all(int $page = 0, int $limit = 50): array
{
return self::$repository->getByFilter(page: $page, limit: $limit);
}

public static function joinWith(string ...$tables): Query
{
$tables[] = self::$repository->getMapper()->getTable();
Expand Down
18 changes: 18 additions & 0 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,24 @@ public function testActiveRecordFilter()
$this->assertNull($model[1]->getDeletedAt()); // Because it was not set in the initial insert outside the ORM
}

public function testActiveRecordEmptyFilter()
{
ActiveRecordModel::initialize($this->dbDriver);

$model = ActiveRecordModel::filter(new IteratorFilter());

$this->assertCount(3, $model);
}

public function testActiveRecordAll()
{
ActiveRecordModel::initialize($this->dbDriver);

$model = ActiveRecordModel::all();

$this->assertCount(3, $model);
}

public function testActiveRecordNew()
{
ActiveRecordModel::initialize($this->dbDriver);
Expand Down

0 comments on commit 6d6c020

Please sign in to comment.