Skip to content

Commit

Permalink
Added \LeanOrm\Model::fetchOneByPkey($id, $relations_to_include = [])…
Browse files Browse the repository at this point in the history
…: ?\GDAO\Model\RecordInterface and \LeanOrm\Model\Collection::getData(): array
  • Loading branch information
rotimi committed May 13, 2023
1 parent fca2b03 commit edc6a63
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/LeanOrm/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,22 @@ public function fetchOneRecord(?object $select_obj=null, array $relations_to_inc

return $result;
}

/**
* Convenience method to fetch one record by the specified primary key value.
*
* @param string|int $id
* @param array $relations_to_include
*
* @return \GDAO\Model\RecordInterface|null
*/
public function fetchOneByPkey($id, $relations_to_include = []): ?\GDAO\Model\RecordInterface {

$select = $this->getSelect();
$select->where(" {$this->getPrimaryCol()} = ? ", [$id]);

return $this->fetchOneRecord($select, $relations_to_include);
}

/**
* {@inheritDoc}
Expand Down
14 changes: 14 additions & 0 deletions src/LeanOrm/Model/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ public function toArray(): array {
return $result;
}

/**
* @return array an array where each value is the result of calling getData() on each record in the collection
*/
public function getData(): array {

$rows = [];
foreach ($this->data as $row) {

$rows[] = $row->getData();
}

return $rows;
}

/////////////////////
// Interface Methods
/////////////////////
Expand Down
33 changes: 31 additions & 2 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,35 @@ public function testThatSetModelWorksAsExpected() {
);
} // public function testThatSetModelWorksAsExpected()

public function testThatGetDataWorksAsExpected() {

$model = new \LeanOrm\Model(
static::$dsn, static::$username ?? "", static::$password ?? "", [], 'author_id', 'authors'
);
$collection = new \LeanOrm\Model\Collection($model);

// empty collection
self::assertEquals([], $collection->getData());

$timestamp = date('Y-m-d H:i:s');
$newRecord1 = $model->createNewRecord([
'name' => 'Author 1 for getData Testing',
'm_timestamp' => $timestamp,
'date_created' => $timestamp,
]);
$newRecord2 = $model->createNewRecord([
'name' => 'Author 2 for getData Testing',
'm_timestamp' => $timestamp,
'date_created' => $timestamp,
]);

$collection[] = $newRecord1;
$collection[] = $newRecord2;
$getDataOnCollection = $collection->getData();
self::assertContains($newRecord1->getData(), $getDataOnCollection);
self::assertContains($newRecord2->getData(), $getDataOnCollection);
}

public function testThatToArrayWorksAsExpected() {

$model = new \LeanOrm\Model(
Expand Down Expand Up @@ -1270,13 +1299,13 @@ public function testThat__unsetWorksAsExpected() {
$timestamp = date('Y-m-d H:i:s');
$newRecord1 = $model->createNewRecord([
//'author_id' => 777, // new record, no need for primary key
'name' => 'Author 1 for toArray Testing',
'name' => 'Author 1 for __unset Testing',
'm_timestamp' => $timestamp,
'date_created' => $timestamp,
]);
$newRecord2 = $model->createNewRecord([
//'author_id' => 777, // new record, no need for primary key
'name' => 'Author 2 for toArray Testing',
'name' => 'Author 2 for __unset Testing',
'm_timestamp' => $timestamp,
'date_created' => $timestamp,
]);
Expand Down
45 changes: 45 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,51 @@ public function testThatFetchOneRecordWorksAsExpected() {
self::assertNull($emptyModel->fetchOneRecord());
}

public function testThatFetchOneByPkeyWorksAsExpected() {

$authorsModel = new LeanOrm\TestObjects\AuthorsModel(static::$dsn, static::$username ?? "", static::$password ?? "");

////////////////////////////////////////////////////////////////////////
// fetch Author with author_id = 2 & include posts during the fetch
// fetch first record in the table
$secondAuthorInTheTable = $authorsModel->fetchOneByPkey(2, ['posts']);
self::assertInstanceOf(GDAO\Model\RecordInterface::class, $secondAuthorInTheTable);
self::assertEquals('2', $secondAuthorInTheTable->getPrimaryVal().'');
self::assertArrayHasKey('author_id', $secondAuthorInTheTable->getData());
self::assertArrayHasKey('name', $secondAuthorInTheTable->getData());
self::assertArrayHasKey('m_timestamp', $secondAuthorInTheTable->getData());
self::assertArrayHasKey('date_created', $secondAuthorInTheTable->getData());

// test that eager loaded relationship data works
self::assertInstanceOf(GDAO\Model\CollectionInterface::class, $secondAuthorInTheTable->posts);
self::assertCount(2, $secondAuthorInTheTable->posts);
self::assertCount(1, $secondAuthorInTheTable->one_post);

self::assertEquals(['2', '4'], $secondAuthorInTheTable->posts->getColVals('post_id'));
self::assertEquals(['2', '2'], $secondAuthorInTheTable->posts->getColVals('author_id'));
self::assertEquals(['Post 2', 'Post 4'], $secondAuthorInTheTable->posts->getColVals('title'));
self::assertEquals(['Post Body 2', 'Post Body 4'], $secondAuthorInTheTable->posts->getColVals('body'));

self::assertEquals(['2'], $secondAuthorInTheTable->one_post->getColVals('post_id'));
self::assertEquals(['2'], $secondAuthorInTheTable->one_post->getColVals('author_id'));
self::assertEquals(['Post 2'], $secondAuthorInTheTable->one_post->getColVals('title'));
self::assertEquals(['Post Body 2'], $secondAuthorInTheTable->one_post->getColVals('body'));

///////////////////////////////////////////////////////////////////////
// Test that record not in db returns null
$authorNotInTheTable = $authorsModel->fetchOneByPkey(777, ['posts']);
self::assertNull($authorNotInTheTable);

$author2NotInTheTable = $authorsModel->fetchOneByPkey(77);
self::assertNull($author2NotInTheTable);

/////////////////////////////
// Querying an empty table
$emptyModel = new $this->modelClass(static::$dsn, static::$username ?? "", static::$password ?? "", [], 'id', 'empty_data' );

self::assertNull($emptyModel->fetchOneByPkey(777));
}

public function testThatFetchPairsWorksAsExpected() {

$tagsModel = new LeanOrm\TestObjects\TagsModel(static::$dsn, static::$username ?? "", static::$password ?? "");
Expand Down

0 comments on commit edc6a63

Please # to comment.