Skip to content

Commit d86a968

Browse files
committed
adjusted behavior and comments around find() method
1 parent 77c3c2f commit d86a968

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"phpunit/phpunit": "^9.0",
2727
"squizlabs/php_codesniffer": "^3.8",
2828
"rregeer/phpunit-coverage-check": "^0.3.1",
29-
"flightphp/runway": "^0.2.4 || ^1.0"
29+
"flightphp/runway": "^0.2 || ^1.0"
3030
},
3131
"autoload": {
3232
"psr-4": {"flight\\": "src/"}

src/ActiveRecord.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ public function getDatabaseEngine(): string
430430
/**
431431
* function to find one record and assign in to current object.
432432
* @param int|string $id If call this function using this param, will find record by using this id. If not set, just find the first record in database.
433-
* @return bool|ActiveRecord if find record, assign in to current object and return it, other wise return "false".
433+
* @return ActiveRecord Returns a hydrated ActiveRecord object if found, otherwise returns an empty ActiveRecord object.
434434
*/
435435
public function find($id = null)
436436
{
437437
if ($id !== null) {
438-
$this->resetQueryData()->eq($this->primaryKey, $id);
438+
$this->eq($this->primaryKey, $id);
439439
}
440440

441441
$this->processEvent('beforeFind', [ $this ]);
@@ -446,6 +446,7 @@ public function find($id = null)
446446

447447
return $result;
448448
}
449+
449450
/**
450451
* function to find all records in database.
451452
* @return array<int,ActiveRecord> return array of ActiveRecord
@@ -577,7 +578,7 @@ public function execute(string $sql, array $params = []): DatabaseStatementInter
577578
* @param array $param The param will be bind to PDOStatement.
578579
* @param ActiveRecord|null $obj The object, if find record in database, will assign the attributes in to this object.
579580
* @param bool $single if set to true, will find record and fetch in current object, otherwise will find all records.
580-
* @return bool|ActiveRecord|array
581+
* @return ActiveRecord|array
581582
*/
582583
public function query(string $sql, array $param = [], ?ActiveRecord $obj = null, bool $single = false)
583584
{

tests/ActiveRecordPdoIntegrationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,18 @@ public function testIsHydratedGoodFind()
542542
$this->assertTrue($user->isHydrated());
543543
}
544544

545+
public function testIsHydratedGoodFindWithSelect()
546+
{
547+
$user = new User(new PDO('sqlite:test.db'));
548+
$user->name = 'bob';
549+
$user->password = 'pass';
550+
$user->save();
551+
$user->select('name')->find(1);
552+
$this->assertTrue($user->isHydrated());
553+
$this->assertEmpty($user->password);
554+
$this->assertEquals('bob', $user->name);
555+
}
556+
545557
public function testIsHydratedGoodFindAll()
546558
{
547559
$user = new User(new PDO('sqlite:test.db'));

0 commit comments

Comments
 (0)